104

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.

8 Answers 8

213

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
}
4
  • 13
    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) Commented Jan 28, 2014 at 7:04
  • 1
    ^ There is a bracket missing in the if statement
    – Aajahid
    Commented Jun 4, 2015 at 6:44
  • 1
    @Aajahid edited :)
    – Rarst
    Commented Jun 4, 2015 at 7:52
  • 1
    If not using multisite, I still prefer the simplicity of current_user_can('editor')
    – Jules
    Commented Jan 25, 2020 at 19:37
15

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 ) );
}
4
  • 1
    works fine for me to get the first role assigned to a user.
    – Q Studio
    Commented Oct 10, 2012 at 19:07
  • What about all the roles assigned to the user? Commented Apr 10, 2017 at 7:14
  • 1
    @Vishal Kumar this will check against all roles assigned to the user. Commented Apr 10, 2017 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
    Commented Nov 16, 2017 at 0:41
4

Calling roles on User Object $user->roles do not return all the roles. The correct way to find if the user has a role or capability is following. (This works in wp version 2.0.0 and greater.) The following function works with user id you can get the current user id by $current_user_id = get_current_user_id();

/**
 * Returns true if a user_id has a given role or capability
 * 
 * @param int $user_id
 * @param string $role_or_cap Role or Capability
 * 
 * @return boolean
 */
function my_has_role($user_id, $role_or_cap) {

    $u = new \WP_User( $user_id );
    //$u->roles Wrong way to do it as in the accepted answer.
    $roles_and_caps = $u->get_role_caps(); //Correct way to do it as wp do multiple checks to fetch all roles

    if( isset ( $roles_and_caps[$role_or_cap] ) and $roles_and_caps[$role_or_cap] === true ) 
       {
           return true;
       }
 }
3

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.

1
  • This is handy when I have to call other methods of the WP_User class. Commented Sep 25, 2019 at 23:18
3
if(current_user_can('administrator')){
   code...
}
1
  • probably the best idea, but might be nice to expend it to any user not just the current one Commented Apr 15, 2022 at 5:56
1

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
}
1

If your site happens to be using WooCommerce, you can simply use:

wc_current_user_has_role( $role );

If not, you can just create the function yourself by copying the way WooCommerce does it (combining wc_current_user_has_role() and wc_user_has_role()):

/**
 * Checks if the current user has a role.
 *
 * @param string $role The role.
 * @return bool
 */
function mysite_current_user_has_role( $role ) {
    /** @var null|WP_User $user */
    $user = wp_get_current_user();

    if ( ! is_object( $user ) ) {
        $user = get_userdata( $user );
    }

    if ( ! $user || ! $user->exists() ) {
        return false;
    }

    return in_array( $role, $user->roles, true );
}
0

This is old post but here is one universal function what working on the all WordPress versions.

if(!function_exists('is_user')):
    function is_user ($role=NULL, $user_id=NULL)
    {
        if(empty($user_id)){
            $user = wp_get_current_user();
        } else {
            if(is_numeric($user_id) && $user_id == (int)$user_id) {
                $user = get_user_by('id', (int)$user_id);
            } else if(is_string($user_id) && $email = sanitize_email($user_id)) {
                $user = get_user_by('email', $email);
            } else {
                return false;
            }
        }
        
        if(!$user) return false;
        
        return in_array( $role, (array)$user->roles, true ) !== false;
    }
endif;

With this function you can search logged in user by role or by user ID/email. Also accept user roles array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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