Determine User Role by Name in WordPress

This function returns the user role by name rather than by capabilities which is useful for determining whether the current user is a Wholesale customer for instance.

It can also return the name of the user role given the user ID as a parameter.

/**
 * Checks if a particular user has a role. 
 * Returns true if a match was found.
 *
 * @param string $role Role name.
 * @param int $user_id (Optional) The ID of a user. Defaults to the current user.
 * @return bool
 */
function appthemes_check_user_role( $role, $user_id = null ) {
 
    if ( is_numeric( $user_id ) )
	$user = get_userdata( $user_id );
    else
        $user = wp_get_current_user();
 
    if ( empty( $user ) )
	return false;
 
    return in_array( $role, (array) $user->roles );
}

Example usage for current user:

// example use for the current user
if ( appthemes_check_user_role( 'wholesaler' )){
   echo "current user is a wholesaler";
}else{
   echo "current user is not a wholesaler";
}

Example usage for given user id:

// example use for a specific user
$user_id = 23;
if ( appthemes_check_user_role( 'wholesaler', $user_id )){
   echo "user $user_id is a wholesaler";
}else{
   echo "user $user_id is not a wholesaler";
}

Original script to be found here:

http://docs.appthemes.com/tutorials/wordpress-check-user-role-function/

All credit to appthemes.com