I have a very specific requirement to show different text in field labels on a user profile page depending on the role of the current user. I can’t seem to find out if the current usage is “author”.

I am looking for a function like:

is_user_in_role($user, "author");

I think this is pretty simple, but I’ve searched for too long without an answer so I thought I’d post it here.

If you only need it for the current user current_user_can() Accepts both roles and abilities.

Update: pass role name current_user_can() is no longer guaranteed to work correctly (see #22624). Instead, you may want to check the user role:

$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
    //The user has the "author" role
}

4

I was looking for a way to get the 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 a is_user_in_role() The function can be implemented like this:

function is_user_in_role( $user_id, $role  ) {
    return in_array( $role, get_user_roles_by_user_id( $user_id ) );
}

4

You can also simply 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 which version get_user_roles_by_user_id was deprecated, but it is no longer an available function.

1

Here’s a function that accepts a user and role for more 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
}

Calling roles on User object $user->roles Do not return all roles. This is the correct way to find out if the user has a role or ability. (It works in wp version 2.0.0 and higher.) Following function works with user id you can get current user id $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;
       }
 }

This is old post but here is a universal function which is working on 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 user id/email. Also accept user role array.