I have a pretty specific requirement to show different text in a field label on the user profile page based on the current user's role. I can't seem to figure out how to check whether the current use is an "author".

I am looking for a function like:

is_user_in_role($user, "author");

I imagine this is pretty simple, but I have searched for too long without an answer so I thought I would post it here.

up vote 86 down vote accepted

If you only need this for current user current_user_can() accepts both roles and capabilities.

UPDATE: Passing a role name to current_user_can() is no longer guaranteed to work correctly (see #22624). Instead, you may wish to check user role:

$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
    //The user has the "author" role
}
  • 9
    I know this post is answered a long time ago but if someone happens to get here... look at the documentation once more for current_user_can() -> "Do not pass a role name to current_user_can(), as this is not guaranteed to work correctly (see #22624). Instead, you may wish to try the check user role function put together by AppThemes." (codex.wordpress.org/Function_Reference/current_user_can) – bestprogrammerintheworld Jan 28 '14 at 7:04
  • 1
    ^ There is a bracket missing in the if statement – Aajahid Jun 4 '15 at 6:44
  • 1
    @Aajahid edited :) – Rarst Jun 4 '15 at 7:52

I was looking for a way to get a user's role using the user's id. Here is what I came up with:

function get_user_roles_by_user_id( $user_id ) {
    $user = get_userdata( $user_id );
    return empty( $user ) ? array() : $user->roles;
}

Then, an is_user_in_role() function could be implemented like so:

function is_user_in_role( $user_id, $role  ) {
    return in_array( $role, get_user_roles_by_user_id( $user_id ) );
}
  • 1
    works fine for me to get the first role assigned to a user. – Q Studio Oct 10 '12 at 19:07
  • What about all the roles assigned to the user? – Vishal Kumar Sahu Apr 10 '17 at 7:14
  • 1
    @Vishal Kumar this will check against all roles assigned to the user. – Stephen M. Harris Apr 10 '17 at 17:34
  • This function does not exist, not sure if it was just old or what, but you should use the answer above or the one I posted below – sMyles Nov 16 '17 at 0:41

You can also just create a new user object:

$user = new WP_User( $user_id );

if ( ! empty( $user->roles ) && is_array( $user->roles ) && in_array( 'Some_role', $user->roles ) ) {
    return true;
}

Not sure what version get_user_roles_by_user_id was removed in, but it's no longer an available function.

Here is a function that accepts a user and role for greater flexibility:

function my_has_role($user, $role) {
  $roles = $user->roles; 
  return in_array($role, (array) $user->roles);
}

if(my_has_role($user, 'some_role')) {
  //do stuff
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.