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?