2

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?

  • It's really strange that WordPress does not show the user's role to the user – Marco Panichi Oct 2 '18 at 6:48
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' );
3

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. enter image description here

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.

enter image description here

  • Ok. So there is nothing built-in already that you're aware of? – mikelovelyuk Sep 28 '15 at 10:35
  • Right, the core display not the role to each user. – bueltge Sep 28 '15 at 10:41
  • What file(s) do I put all this in? – mikelovelyuk Sep 29 '15 at 14:01
  • A custom Plugin is the right way. – bueltge Sep 29 '15 at 19:07
0

I simply use:

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

To display the current user role.

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.