Reputation: 1313
How to get the currently logged in user's role in wordpress?
Upvotes: 18
Views: 52579
Reputation: 2452
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 */
}
Upvotes: 5
Reputation: 53
<?php global $current_user; //get the current user
echo $current_user->roles[0]); //display the current user's role
?>
Upvotes: 1
Reputation: 2497
function get_role_by_id( $id ) {
if ( !is_user_logged_in() ) { return false; }
$oUser = get_user_by( 'id', $id );
$aUser = get_object_vars( $oUser );
$sRole = $aUser['roles'][0];
return $sRole;
}
Upvotes: 1
Reputation: 875
If you don't know the user id, this function will help you (put it in your theme functions.php file)
function get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
And then, in your template you can get user role by calling get_user_role().
Found it here.
Upvotes: 16
Reputation: 7012
Assuming you have the user id ($user_id) something like this should work:
$user = new WP_User( $user_id );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role )
echo $role;
}
Get the user id from your session.
Upvotes: 21