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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | $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 https://wordpress.stackexchange.com/questions/1665/getting-a-list-of-currently-available-roles-on-a-wordpress-site If you did want to check if the user is in a certain group you could … Read more