WordPress

Add a WordPress Menu To Your WordPress Toolbar

Wordpress Admin Bar This WordPress snippet will add a WordPress Menu on the WordPress Toolbar.

This uses the admin_bar_menu which will run when the admin bar is created. This action calls the function Add_Menu_To_Toolbar.

Inside this function we start by grabbing the the WordPress menu "Toolbar Menu" by using the function wp_get_nav_menu_object(). This will return the menu object, from this object we can get the ID for the menu to get all the menu items by using the function wp_get_nav_menu_items(). Now we have all the menu items we can loop through them and add the menu items to the toolbar by using the add_node method. Having the Parent attribute will ensure that you can have sub-menus on the toolbar.

add_action( 'admin_bar_menu', 'Add_Menu_To_Toolbar', 500 );

function Add_Menu_To_Toolbar(){

$menu = wp_get_nav_menu_object( "Toolbar Menu" );
$menu_items = wp_get_nav_menu_items( $menu->term_id );
						
   foreach ($menu_items as $items) {
	$args = array( 'id' => 	$items->ID,
		'title' => 	$items->title,
		'parent' =>     $items->menu_item_parent,
		'href' 	=> 	$items->url,
		'meta' 	=> 	FALSE
	);
				
	$wp_admin_bar->add_node( $args );
   }
}
Back to top ^

Join Paulund

  • Get access to 100s of premium tutorials and downloadable content
  • Members content consists of Premium WordPress plugins, CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
  • In-depth development tutorials
  • Priority tutorial requests
  • Priority email development support
  • No ads

Here at Paulund you'll find Web Development tutorials and snippets focusing mainly on PHP, WordPress and CSS. Members will get access to premium tutorials that go into more detail about development with these languages. You will also get access to downloadable premium content based on the tutorial.

Join Paulund

Leave a Reply

Your email address will not be published. Required fields are marked *