in 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 );
   }
}

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email