# Show Your Role in the Toolbar

> Adds the current user's role next to their name in the admin toolbar ("Howdy, Sam (Editor)"), helpful when testing with several accounts.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: roles, admin-bar, toolbar
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/role-in-toolbar-greeting/

## Code

```php
add_action( 'admin_bar_menu', function ( $bar ) {
    $node = $bar->get_node( 'my-account' );

    if ( ! $node || ! is_user_logged_in() ) {
        return;
    }

    $names = array();

    foreach ( (array) wp_get_current_user()->roles as $role ) {
        $names[] = isset( wp_roles()->role_names[ $role ] ) ? translate_user_role( wp_roles()->role_names[ $role ] ) : $role;
    }

    if ( $names ) {
        $node->title = preg_replace( '/(<span class="display-name">)(.*?)(<\/span>)/', '$1$2 (' . esc_html( implode( ', ', $names ) ) . ')$3', $node->title, 1 );
        $bar->add_node( (array) $node );
    }
}, 999 );
```

## Notes

Shows all roles when a user has more than one.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Show Your Role in the Toolbar" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
