This is an old question but I thought I’d add this 2023 option for getting the currently logged in user’s role in WordPress. By default, WordPress users only have one role, but there are plugins and themes that can extend that feature giving users multiple roles, so I wanted to provide options for that too.
First, add this function somewhere to your website, probably at the bottom of your active theme’s functions.php file.
/**
* Get Current User Roles
*
* @param bool $return_array Optional. If true, will return an array of all roles. Otherwise, will return just the first role. Default is false.
*
* @return string|array Returns the current user's role(s) based on parameters. Empty string or array if user is not logged in or has no roles.
*/
function au_get_current_user_roles($return_array = false) {
// Retrieves the current user
$current_user = wp_get_current_user();
// Checks if user is logged in and has roles
if($current_user->exists() && count($current_user->roles)) {
if($return_array) { return $current_user->roles; } // Return all roles as array
return $current_user->roles[0]; // Return just the first role as a string
}
if($return_array) { return array(); } // Return empty array
return ''; // Return empty string
}
To use the function to check if a user has a specific role (e.g. administrator) where users can only have one role, you’d use the function like this:
if('administrator' == au_get_current_user_roles()) {
// Do something for admins only
}
Now if your users can have multiple roles and you want to check if they have a specific one (e.g. administrator), you can do it like this:
if(in_array('administrator', au_get_current_user_roles(true))) {
// Do something for admins only
}
Now those examples above just checked if a user is a specific role (administrator), but what if you want to do something for users that are either an admin or an editor? Or both? You can do something like this:
$allowed_roles = array('administrator', 'editor');
if(count(array_intersect($allowed_roles, au_get_current_user_roles(true)))) {
/* Do something for users who are admins, users who
are editors, and users who are both admin and users */
}
Assuming you have the user id ($user_id) something like this should work:
Get the user id from your session.
If you don’t know the user id, this function will help you (put it in your theme functions.php file)
And then, in your template you can get user role by calling get_user_role().
Found it here.
This is an old question but I thought I’d add this 2023 option for getting the currently logged in user’s role in WordPress. By default, WordPress users only have one role, but there are plugins and themes that can extend that feature giving users multiple roles, so I wanted to provide options for that too.
First, add this function somewhere to your website, probably at the bottom of your active theme’s functions.php file.
To use the function to check if a user has a specific role (e.g.
administrator
) where users can only have one role, you’d use the function like this:Now if your users can have multiple roles and you want to check if they have a specific one (e.g.
administrator
), you can do it like this:Now those examples above just checked if a user is a specific role (
administrator
), but what if you want to do something for users that are either an admin or an editor? Or both? You can do something like this: