WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to get the role associated with a user -- not the "currently logged in user".

I am using Buddypress (not that should matter to the nature of this question) and I am in the bp_members() loop.

How can I retrieve the role of the user I am reporting on in the loop at any given time?


share|improve this question

Use the user ID and WP_User:

$user = new WP_User( $user_id );
print wp_sprintf_l( '%l', $user->roles );


 * Get user roles by user ID.
 * @param  int $id
 * @return array
function wpse_58916_user_roles_by_id( $id )
    $user = new WP_User( $id );

    if ( empty ( $user->roles ) or ! is_array( $user->roles ) )
        return array ();

    $wp_roles = new WP_Roles;
    $names    = $wp_roles->get_names();
    $out      = array ();

    foreach ( $user->roles as $role )
        if ( isset ( $names[ $role ] ) )
            $out[ $role ] = $names[ $role ];

    return $out;

Usage example:

print '<pre>' 
    . htmlspecialchars( 
        print_r( wpse_58916_user_roles_by_id(1), TRUE ) 
    . '</pre>';

    [administrator] => Administrator
share|improve this answer
Thanks @toscho -- it didn't seem to get me what I was expecting. I got an editor, a blank/null value, and bbp_participant. I was expecting to see Admin, Editor, Author or Subscriber as those are the only roles we are using. – frank13 Jul 18 '12 at 20:04
@frank13 See my update. – toscho Jul 18 '12 at 20:15
Thanks @toscho ... that's a lot of code to get a data value I would have thought would have been more readily available. – frank13 Jul 18 '12 at 20:34
Very helpful, thanks! So sad, and confusing, why you can't access the user's role more directly. The "can user" function functions the opposite of what most people would want. – Mike Sep 3 '13 at 22:44

If bp_members returns an array of WP_User objects, each should have a the property roles that contains an array of the users roles.

Otherwise, you can use get_user_meta for the key wp_capabilities, which will return an array of role => capability pairs:

$caps = get_user_meta($user->ID, 'wp_capabilities', true);
$roles = array_keys((array)$caps);
share|improve this answer
$roles ends up being an array. I am looking to get the $user->ID's role. Only one role as no users in our system are assigned more than 1 role. – frank13 Jul 18 '12 at 20:07
$roles[0], done. – chrisguitarguy Jul 18 '12 at 20:09
Ok, thanks @ChristopherDavis ... $roles[0] returns "0" across the board for every user in the list – frank13 Jul 18 '12 at 20:15
@chrisguitarguy does $roles[0] work still? – tazo todua May 28 '15 at 10:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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