wp admin - Displaying which Role the current user is assigned to

admin2025-06-04  3

Is there a section on the admin panel of my WordPress installation which just displays what role the current logged in user has?

For example, I know I have administrator permissions because I know that but I can't see it confirmed anywhere.

Also, if I set myself up as an Editor, how do I quickly check I am definitely logged in as an Editor? (apart from just "knowing").

It would be really nice just to know that in case you're doing 10 things at once and lose track.

Maybe it would be nice to display that after the "How are you.." in the top right.

EDIT

Before anyone jumps in and mentions the Users panel. Yes, I can see there is the Role column (as I am an admin), but someone who was just a Contributor wouldn't be able to see that, right?

Is there a section on the admin panel of my WordPress installation which just displays what role the current logged in user has?

For example, I know I have administrator permissions because I know that but I can't see it confirmed anywhere.

Also, if I set myself up as an Editor, how do I quickly check I am definitely logged in as an Editor? (apart from just "knowing").

It would be really nice just to know that in case you're doing 10 things at once and lose track.

Maybe it would be nice to display that after the "How are you.." in the top right.

EDIT

Before anyone jumps in and mentions the Users panel. Yes, I can see there is the Role column (as I am an admin), but someone who was just a Contributor wouldn't be able to see that, right?

Share Improve this question asked Sep 28, 2015 at 10:06 mikelovelyukmikelovelyuk 1211 gold badge1 silver badge4 bronze badges 1
  • It's really strange that WordPress does not show the user's role to the user – Marco Panichi Commented Oct 2, 2018 at 6:48
Add a comment  | 

3 Answers 3

Reset to default 4

As you suggested, here's how you can display the roles next to the username in the admin bar:

function wpse_203917_admin_bar_menu( $wp_admin_bar ) {
    if ( ! $node = $wp_admin_bar->get_node( 'my-account' ) )
        return;

    $roles = wp_get_current_user()->roles;

    $node->title .= sprintf( ' (%s)', implode( ', ', $roles ) );

    $wp_admin_bar->add_node( $node );
}

add_action( 'admin_bar_menu', 'wpse_203917_admin_bar_menu' );

You check for the role for the current user and add the value to the admin bar, like the profile item.

To get the role of the current user use the small function below:

/**
 * Returns the translated role of the current user. 
 * No role, get false.
 *
 * @return string The translated name of the current role.
 **/
function fb_get_current_user_role_fixed() {
    global $wp_roles;

    $current_user = wp_get_current_user();
    $roles = $current_user->roles;
    $role = array_shift( $roles );

    return isset( $wp_roles->role_names[ $role ] ) ? translate_user_role( $wp_roles->role_names[ $role ] ) : FALSE;
}

Also a example to add the value from the function to the admin bar, in my example a new entry to the account item in the Admin Bar.

add_action( 'admin_bar_menu', 'fb_change_admin_bar_item' );
/**
 * Add item to the admin bar, to the my-account item.
 *
 * @param Array $wp_admin_bar
 */
function fb_change_admin_bar_item( $wp_admin_bar ) {

    $args = array(
        'id'     => 'user_role',
        'title'  => __( 'Role:' ) . ' ' .     fb_get_current_user_role_fixed(),
        'parent' => 'my-account' 
    );
    $wp_admin_bar->add_node( $args );
}

See the result as screenshot, much easier to understand the goal of the example.

As alternative the second example, that add the role name to the user name, on the default title in the admin bar.

add_action( 'admin_bar_menu', 'fb_change_admin_bar_item' );
function fb_change_admin_bar_item( $wp_admin_bar ) {

    $node = $wp_admin_bar->get_node( 'my-account' );

    if ( ! $node ) {
        return $wp_admin_bar;
    }

    $node->title .= ' ' . fb_get_current_user_role_fixed();

    $wp_admin_bar->add_node( $node );
}

Also here a screenshot of the result.

I simply use:

global $current_user; echo array_shift($current_user->roles);

To display the current user role.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748974635a315299.html

最新回复(0)