13

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?

Thanks.

16

Use the user ID and WP_User:

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

Update

/**
 * 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>';

Array
(
    [administrator] => Administrator
)
  • 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. – fuxia Jul 18 '12 at 20:15
  • 2
    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
7

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:

<?php
$caps = get_user_meta($user->ID, 'wp_capabilities', true);
$roles = array_keys((array)$caps);
  • $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? – T.Todua May 28 '15 at 10:14
0

As I understand, it probably changed over the years, now it's not 'wp_capabilities'. I looked at the core WP files, how they do it now, and found a new solution to this. Here's the code I use now:

global $wpdb;
$cap = get_user_meta( $wp_user_id, $wpdb->get_blog_prefix() . 'capabilities', true );

$cap then comes as an associative array, for example:

{
    "administrator": true
}

or

{
    "subscriber": true
}

So in your case you should probably then get the keys of the array:

$caps_array = array_keys( $cap );

and then get everything from it. I needed to see if the user has the administrator role, so I checked:

if ( is_array( $cap ) && !empty( $cap['administrator'] ) ) { return true; }

I had a user ID, not the whole WP_User object, and I didn't want to use any solution that would create the user object. As I checked, get_user_meta goes straight to the db to check for this exact parameter, so this should be the fastest and resource-wise route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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