1

I currently have this snippet:

$user = new WP_User(get_current_user_id()); 
echo $user->roles[1];

and the output the slug of the bbPress forum role. (roles[0] would be the general WP role but I don't need that.)

What I need is the role name, not the slug. So, the expected output should be something like "Keymaster", "Participant", "Spectator" etc.

So, how do I get the Role Name of the current user?

  • The function wp_get_current_user() should helps you. You get return all data to the user via the class ´WP_User`. – bueltge Sep 19 '16 at 12:31
3

I'm not sure if bbPress follows WordPress conventions, but WP has a global class called $WP-roles that holds the role information. So, starting from what you have, there is the role of the current user:

$current_role = $user->roles[1];

Next, retrieve a list of all roles:

$all_roles = $wp_roles->roles; 

Then, loop through $all_roles and find the $current_role":

foreach ($all_roles as $role_key => $role_details) {
  if ($role_key == $current_role) $current_role_name = $role_details['name'];
  }

Now, $current_role_name should hold the display name you're looking for (I didn't check this code, however).

| improve this answer | |
  • The result is a warning that says: Invalid argument supplied for foreach() – booster Sep 20 '16 at 22:01
  • I don't use bbPress, so I don't know if it does something to the roles structure. If you use var_dump on $all_roles you should be able to see its structure and adapt the foreach loop with the right arguments. – cjbj Sep 21 '16 at 7:17
2

thnx for this code snippet! I'm running a Buddypress install (and custom theme) where it is possible to add multiple roles to one user. I adjusted your code a little bit for that to work:

<?php
//Get role name by user ID
if( !function_exists('get_user_role_name') ){
    function get_user_role_name($user_ID){
    global $wp_roles;

        $user_data = get_userdata($user_ID);
        $user_role_slug = $user_data->roles;

        foreach($user_role_slug as $user_role){
            //count user role nrs
            $user_role_nr++;
            //add comma separation of there is more then one role 
            if($user_role_nr > 1) { $user_role_list .= ", "; }
            //add role name 
            $user_role_list .= translate_user_role($wp_roles->roles[$user_role]['name']);
        }

    //return user role list
    return $user_role_list;

    }
}
?>
| improve this answer | |
0

Here is the way to get it

function get_user_role($user_id) {
    global $wp_roles;

    $roles = array();
    $user = new WP_User( $user_id );
    if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
    foreach ( $user->roles as $role )
        $roles[] .= translate_user_role( $role );
    }
    return implode(', ',$roles);
} 
//then call the function with userid param
echo get_user_role( 7 );
| improve this answer | |
  • The code in its current form tries to translate slug, which is a poor solution. You should pass role name to function translate_user_role(). – nmr Apr 25 '19 at 8:33
0

You can get current user role name(translatable name but not slug) by following function, you just need to pass current user role slug as a parameter:

function wp_get_current_user_translatable_role_name( $current_user_role_slug = '' ) {
    $role_name = '';

    if ( ! function_exists( 'get_editable_roles' ) ) {
        require_once ABSPATH . 'wp-admin/includes/user.php';
    }

    // Please note that translate_user_role doesn't work in the front-end currently.
    load_textdomain( 'default', WP_LANG_DIR . '/admin-' . get_locale() . '.mo' );

    $editable_roles = array_reverse( get_editable_roles() );

    foreach ( $editable_roles as $role => $details ) {
        $name = translate_user_role( $details['name'] );
        // preselect specified role
        if ( $current_user_role_slug == $role ) {
            $role_name  = $name;
        }
    }

    echo $role_name ;
}

Now, get current user role slug name by following code:

$user_meta = get_userdata(get_current_user_id());
$current_user_role_slug = $user_meta->roles[0];

Use wp_get_current_user_translatable_role_name( $current_user_role_slug ); function to get current user role name(translatable name but not slug).

NOTE: User must have to login to view role name.

| improve this answer | |
  • Quotation: "What I need is the role name, not the slug. " – nmr Apr 25 '19 at 7:07
  • @nmr you can just echo variable name like that: <?php echo ucfirst($role_name); ?> this should output like "Keymaster", "Participant", "Spectator", "administrator" etc. – Razon Komar Pal Apr 28 '19 at 4:56
  • But it's still a slug. What if the role will be like slug='spectator_vip' name= 'VIP Spectator' or slug= 'shop_manager' name= 'Shop Manager'. If user has a role "editor" (slug of built in role), in the user list, depending on the active language, you can see "Editor", "Redaktor", "редактор". You can not assume that the name is equal to the slug. – nmr Apr 28 '19 at 5:33
  • @nmr see my modified code, i just misunderstand the point of question but now i understand the point and fix it. – Razon Komar Pal Apr 28 '19 at 7:48
0

Maybe they've already solved this issue. But, now I just wrote this and I decided to share it.

<?php
//Get role name by user ID
if( !function_exists('get_user_role_name') ){
    function get_user_role_name($user_ID){
        global $wp_roles;

        $user_data = get_userdata($user_ID);
        $user_role_slug = $user_data->roles[0];
        return translate_user_role($wp_roles->roles[$user_role_slug]['name']);
    }
}
?>

<?php echo get_user_role_name(User ID here);?>
| improve this answer | |

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.