0

I'm trying to find out whether the current user is in the allowed list and if so let them view the page. If not then deny them the page. Because the user may be in two user levels I am adding them to an array and then checking it against an array of allowed users.

This is the code i'm using in the page.php file:

// Check the category and whether it is a child of 5
foreach((get_the_category()) as $childcat) {
  if (cat_is_ancestor_of(5, $childcat)) { 

            // define arrays
    $inRoles = array();
    $allowedToAccess = array('administrator','gold','pmpro_role_4');

            // Check if user is logged in and get role
    if ( is_user_logged_in() ) {
        $user = new WP_User( $user_ID );
        if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
            foreach ( $user->roles as $role ) {

                // Add current roles to their roles array
                array_push($inRoles, $role);            

                var_dump($inRoles);


                 // check if role(s) is/are allowed
                if (in_array($inRoles, $allowedToAccess)) {
                    echo "Yes, You are ALLOWED TO ACCESS THIS PAGE";
                } else {
                    echo "Nope, you can not access this page";  
                }
            }
        }
    }
  }
}

The var_dumpthat you can see there outputs this:

array(1) { [0]=> string(13) "administrator" }

so, as you can see the user is an administrator. However, for some reason the output on the page is:

Nope, you can not access this page

Can anyone see why this is?

1 Answer 1

2

Try this out

// Check the category and whether it is a child of 5
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(5, $childcat)) { 

        // define arrays
$inRoles = array();
$allowedToAccess = array('administrator','gold','pmpro_role_4');

        // Check if user is logged in and get role
 if ( is_user_logged_in() ) {
    $user = new WP_User( $user_ID );
    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {

        foreach ( $user->roles as $role => $name ) {

            // Add current roles to their roles array
            array_push($inRoles, $name);            

            var_dump($inRoles);


             // check if role(s) is/are allowed
            if(in_array($inRoles[0],$allowedToAccess)) 
            {

               echo "Yes, You are ALLOWED TO ACCESS THIS PAGE";
            } 
          else 
            {
                echo "Nope, you can not access this page";  
            }
        }



    }
  }
 }
 }
Sign up to request clarification or add additional context in comments.

4 Comments

@user3596752: can you show me print_R($inRoles). which is genrated by my code
that gives: Array ( [0] => administrator )
@user3596752: now try this out again
@user3596752: if you don't want to use the array_intersect then use this if(in_array($inRoles[0],$allowedToAccess)) { }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.