![]()
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 );
}
}
