Comments on: Get current user role in WordPress https://pluginrepublic.com/get-current-user-role-in-wordpress/ WooCommerce Plugins Mon, 16 Nov 2020 21:37:00 +0000 hourly 1 By: Chris Jeffries https://pluginrepublic.com/get-current-user-role-in-wordpress/#comment-60741 Sat, 25 Jan 2020 02:07:32 +0000 https://pluginrepublic.com/?p=2085#comment-60741 If there are multiple permitted roles and you want to know if the user has any of them:
function current_user_is_permitted($permitted_roles) {
if (in_array(‘*’, $permitted_roles)) { //* is the super-permission
return true;
}
$user = wp_get_current_user();
$roles = ( $user instanceof WP_User )? $user->roles: [‘visitor’];
return (count(array_intersect($roles, $permitted_roles))>0);
}

]]>
By: Roland https://pluginrepublic.com/get-current-user-role-in-wordpress/#comment-48830 Wed, 27 Nov 2019 12:47:37 +0000 https://pluginrepublic.com/?p=2085#comment-48830 In reply to Gareth.

Correct, otherwise only the first assigned role will be returned and not all assigned roles as multi-role assignment is possible.

]]>
By: Gareth https://pluginrepublic.com/get-current-user-role-in-wordpress/#comment-13491 Wed, 17 Oct 2018 06:04:32 +0000 https://pluginrepublic.com/?p=2085#comment-13491 In reply to Mr Alexander.

You can see that $user->roles is actually an array. Just return that instead of the first element.

]]>
By: Mr Alexander https://pluginrepublic.com/get-current-user-role-in-wordpress/#comment-13473 Wed, 17 Oct 2018 01:23:11 +0000 https://pluginrepublic.com/?p=2085#comment-13473 What about the event that a user has multiple roles?

]]>
By: Deepak https://pluginrepublic.com/get-current-user-role-in-wordpress/#comment-9970 Mon, 02 Jul 2018 07:47:46 +0000 https://pluginrepublic.com/?p=2085#comment-9970 I want to get two different login form for two different roles on two different pages.

]]>