Get current user role in WordPress

User roles and capabilities in WordPress are something I’ve been looking at recently for the WooCommerce Members Only plugin. Members Only allows you to create a membership site in WooCommerce.
One thing I need to check is the user’s role and whether that grants them permission to view certain content.
There’s not actually a WordPress function to give you the role of a user so it’s necessary to concoct your own. This is what I used for WooCommerce Members Only when checking if a user can view a page or post:
<?php | |
/** | |
* Get the user's roles | |
* @since 1.0.0 | |
*/ | |
function wcmo_get_current_user_roles() { | |
if( is_user_logged_in() ) { | |
$user = wp_get_current_user(); | |
$roles = ( array ) $user->roles; | |
return $roles; // This returns an array | |
// Use this to return a single value | |
// return $roles[0]; | |
} else { | |
return array(); | |
} | |
} |
First off, we check that the user is actually logged in. If they’re not logged in, they won’t have a role assigned.
If the user is logged in, we use wp_get_current_user
to return the WP_User
object. This provides us with a stack of information about the data and we can access their user role(s) via $user->roles
. Note that I’ve cast this to an array for safety.
At this stage, you can choose how to return the roles. In the example above, I return the roles as an array – because it might be the case that your user has more than one role. For instance, they could be a subscriber and a customer.
However, if you are confident that they will just have one role, then use the line below that’s been commented out. This will return the first element in the array, which will be the user’s role. If you’re expecting the user to be assigned more than one role you return the array as is.
Further reading
If user roles are your bag, you might find these articles of interest:
- How to set prices by user role in WooCommerce (2 easy steps)
- How to create WooCommerce protected categories (by user role or password)
- Detailed information on WordPress user roles (external site)
Has this article been useful to you?
If you’ve found the code in this article to be helpful, please take a moment to check out the Members Only plugin by clicking the image below.
I want to get two different login form for two different roles on two different pages.
What about the event that a user has multiple roles?
You can see that
$user->roles
is actually an array. Just return that instead of the first element.Correct, otherwise only the first assigned role will be returned and not all assigned roles as multi-role assignment is possible.
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);
}