A Simple Guide to Customizing the WordPress Admin ToolBar

A lot of people have Love-hate relationship with the WordPress Admin Toolbar. I personally love the toolbar as it makes life easy for me. I spend a lot of time working with WordPress and would simply go mad it there is no toolbar. It makes super easy to switch between the Front end and the Back End.

However there might be some cases where you might want to customize or all together remove the toolbar.  For example you might encounter a situation where you want to disable toolbar based on user roles. In this post I cover a few techniques which can be used to customize the WordPress Admin ToolBar. In other

More specifically I will cover the following use cases.

  • Disable Admin Bar for all users
  • Disable admin bar based on user roles
  • Display Admin Toolbar to Logged Out Users
  • Add / Remove Custom Menu Item / Links  to Admin ToolBar

Now if you simple want to hide the Admin Toolbar from the front end, you can do so by going to Users-> Your Profile.    However if you need more control over the toolbar you will need to dabble into code and this is where this post comes in .

Lets start.

Disable Admin Bar for all users.

Add the following snippet to your theme’s function.php file. This snippet will hide the toolbar from the front-end view for all users. It will also  override the settings in the user profile.

Here we make use of the show_admin_bar filter.

add_filter('show_admin_bar', '__return_false'); 
// This snippet removes toolbar for every user

 

Disable Admin Bar based on user roles.

Lets say you want to disable admin toolbar for all users expect admin and editors.   We can simply wrap the show_admin_bar filter in a condition. Refer to the following snippet.

// Only Administrators and Editors can edit Pages. 
// So we utilize edit_pages capability 
if (!current_user_can('edit_pages')) {
        add_filter('show_admin_bar', '__return_false');
    } 

The code is pretty self explanatory.  We make use of the edit_page capability to check if the current user is Admin or Editor.   Similarly you can disable admin toolbar for users based on their capabilities and roles. Here is the link to official documentation

 

Display Admin ToolBar to logged out users

Ok. Till now we were talking about removing the toolbar. What if you want to display admin bar to all users including logged out users?   Simply insert the following snippet in your functions .php file . This snippet ensures that the Toolbar is visible all the time.

//add_filter( 'show_admin_bar', '__return_true' );

This is what the logged out user will see

Admin ToolBar Logged Out Users

As you can see that logged out users don’t have access to the menu items like New Post , User Profile etc. What if you want to add custom menu items / links to the toolbar? We will cover it in the next point

How to Add / Remove links to the Admin Tool-Bar

Lets say you want to remove remove the Logo, Update and the Comment Menu from the toolbar. You can easily do so via following snippet

add_action( 'admin_bar_menu', 'remove_links_toolbar', 999 );

function remove_links_toolbar($wp_admin_bar) 
{
 global $wp_admin_bar;
 $wp_admin_bar->remove_menu('comments');
 $wp_admin_bar->remove_menu('updates');
 $wp_admin_bar->remove_menu('logo');
}

This is how the toolbar will appear now

ToolBar Remove Links

As you can see we hooked into admin_menu_bar function and utilized the $wp_admin_bar object to remove comments and Updates link. Take note that we cannot pass array in remove_menu, hence we removed the items one by one.

Its also very easy to Add Custom links to the admin toolbar. This function can be very beneficial if you make websites for client.

In this example I will add a menu MY Page to the toolbar. We will utilize the add_node method to create a new menu item. Take note that the following snippet will create a parent level menu item.

add_action( 'admin_bar_menu', 'toolbar_link_to_mypage', 999 );

function toolbar_link_to_mypage( $wp_admin_bar ) {
 $args = array(
 'id' => 'my_page',
 'title' => 'My Page',
 'href' => 'http://mysite.com/my-page/'
 
 );
 $wp_admin_bar->add_node( $args );
}

 

ToolBar with Custom Menu Item

Conclusion

As you can that you dont need to be a code wizard to customize the wordpress toolbar. Using basic knowledge of wordpress filters we can customize the admin tool bar any which way we want.

Even though it is quite simple, I understand that not everybody is comfortable with editing code. So in my next blog post I will cover various WordPress plugins which can help you in customizing the Admin Toolbar. Till Then Have a great Day!!

Leave a Comment