6

Customize Admin Bar in WordPress

WorPress adds a fixed menu bar (Admin Bar aka Toolbar) at the top of the screen when user is logged in and the menu bar is visible even when you are at the front end of the site so it’s very helpful to access some admin pages quickly. WordPress allows us to customize the bar and it’s possible to add new links (menu items) in that bar or remove or change the existing ones. Using admin_bar_menu or wp_before_admin_bar_render hook we can customize the Admin Bar. To add new items we can call add_menu() method of the $wp_admin_bar global object (an instance of WP_Admin_Bar class) which may available only during the admin_bar_menu and wp_before_admin_bar_render hooks. In most cases developers use WP_Admin_Bar hook (WP_Admin_Bar’s internal hook) but also it could be done using wp_before_admin_bar_render hook as well and this action hook is called before $wp_admin_bar object is used to render the admin bar to the screen.

Note: The Admin Bar is replaced with the toolbar since WordPress Version 3.3. The preferred way to add or remove items to/from the toolbar is with add_node(), add_group() and remove_node(), which are almost similar.

How to use add_menu() :

How to use remove_menu() :

Add a new top level menu using wp_before_admin_bar_render hook :

Add a new top level menu and two sub menus using admin_bar_menu hook :

The ability to customize the admin menu bar is very useful and common use to WordPress plugin authors to add a shortcut link of their plugin’s setup page in the admin bar just like I’ve used in the above example to show my plugin’s setup page using the admin_url() function to set the url of my plugin’s setup page.

Make an Existing Child Node a Parent Node using admin_bar_menu hook :

Above code will move out the new post link (titled as Post) from the parent menu New and also the title of the link will be changed from Post to Add New Post.

Remove WordPress logo using wp_before_admin_bar_render hook :

To remove an existing item from admin bar admin_bar_menu won’t work instead, wp_before_admin_bar_render hook has to be used.

Also, you can completely hide the admin bar from front end (just place this code in functions.php file)

These lines will only display the admin bar for users with administrative privileges.

Also, you can use set_current_user action hook to hide the admin bar

This function uses do_action(), it calls set_current_user hook after setting the current user. This function could be replaced via plugins. If plugins do not redefine this function, then this will be used instead.

You may change the Howdy to Hello or whatever you want, on admin bar

If you want to completely remove my-account menu from admin bar, try this

Updated On : 20-11-2013

Latest Blog

0

PHP – 8.0 Match Expression

In PHP 8.0 there is a new feature or I should say a new language construct (keyword) going to be introduced,  which has been implemented depending […]

0

New Union Type in PHP – 8

A new RFC has been finished voting and accepted for PHP – 8. That is “Union Types”, which is an extension of PHP’s type system. […]