This code is handy if you want to find out a users role/s. You could expand it and check if a user has a given role or similar.
$user_info = get_userdata(2152);
echo 'Username: ' . $user_info->user_login . "\n";
echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
echo 'User ID: ' . $user_info->ID . "\n";
$allroles = get_editable_roles();
foreach ($allroles as $k => $v) {
echo "<br>role = $k ";
}
function get_editable_roles() {
global $wp_roles;
$all_roles = $wp_roles->roles;
$editable_roles = apply_filters('editable_roles', $all_roles);
return $editable_roles;
}
This code is based on examples from:
https://codex.wordpress.org/Function_Reference/get_userdata
and
If you did want to check if the user is in a certain group you could do something like this:
if (in_array('subscriber', $users_roles_array)) {
// do something...
}
Nice! Can you suggest a way to find the highest level role for a single user? Many times users have multiple roles. I am looking to find and return the highest access, such as editor or administrator.