
Custom Menu Item Position In WordPress Admin Bar (Toolbar)
Published on December 26, 2012

When adding a new item to the WordPress Toolbar it's important to use admin_bar_menu hook. Sure, you can add new items with the wp_before_admin_bar_render hook for example but you cannot set item position. Position of the item is determined by add_action priority value, function with the lowest priority number is executed first.
These are the menus that WordPress adds by default and their priorities (positions):
- wp_admin_bar_wp_menu - 10
- wp_admin_bar_my_sites_menu - 20
- wp_admin_bar_site_menu - 30
- wp_admin_bar_updates_menu - 40
- wp_admin_bar_comments_menu - 60
- wp_admin_bar_new_content_menu - 70
- wp_admin_bar_edit_menu - 80
As you can see above, comments_menu has a priority of 60 and if we were to add a new node with priority 50 it would be placed between Updates and Comments.
functions.phpfunction custom_node_example($wp_admin_bar){ $args = array( 'id' => 'custom-node', 'title' => 'Custom Node', 'href' => 'http://example.com/page/', 'meta' => array( 'class' => 'custom-node-class' ) ); $wp_admin_bar->add_node($args); } add_action('admin_bar_menu', 'custom_node_example', 50);
Priority is the third parameter in add_action, it defaults to 10 if left empty. Don't forget the menu item priorities when applying remove_node() function...
functions.phpadd_action('admin_bar_menu', 'remove_some_nodes', 55); function remove_some_nodes( $wp_admin_bar ) { // Updates will be removed because it's priority is 50 $wp_admin_bar->remove_node('updates'); // Comments will not be removed because it's priority is 60 and doesn't exist yet $wp_admin_bar->remove_node('comments'); }
It's easy to forget things like that when removing nodes so you might wanna use priority 999 or wp_before_admin_bar_render hook for removal.
Comments (1)
Mohammad Kashif on March 11, 2018
Hi
How can i add tool bar user menu in theme navigation?
Thanks