Attaching a navigation menu to the admin bar?

I have seen a number of solutions of how to manually attach links to the new WP admin bar, but I need to make this much easier for my site admins.

It occurred to me that the easiest solution would be to create a custom navigation menu, and then have that menu ‘attached’ to the admin bar. This way the site admin could very easily add new links to the admin bar by simply adding pages to the custom menu.

Read More

The primary idea being to place a dropdown menu displaying the menu pages directly on the right side of the admin bar.

Related posts

Leave a Reply

1 comment

  1. It turns out to be very easy! No need for a special walker, wp_get_nav_menu_items() returns everything you need. This example adds an single root menu item and then the menu, you can do this differently if you want. It maps all extra menu features I could find in the code, I don’t know whether you can set them all in the menu UI.

    add_action( 'admin_bar_menu', 'wpse15186_admin_bar_menu' );
    function wpse15186_admin_bar_menu( &$wp_admin_bar )
    {
        $menu = wp_get_nav_menu_object( 'WPSE 15186 test menu' );
        $menu_items = wp_get_nav_menu_items( $menu->term_id );
    
        $wp_admin_bar->add_menu( array(
            'id' => 'wpse15186-menu-0',
            'title' => 'WPSE 15186 menu',
        ) );
    
        foreach ( $menu_items as $menu_item ) {
            $wp_admin_bar->add_menu( array(
                'id' => 'wpse15186-menu-' . $menu_item->ID,
                'parent' => 'wpse15186-menu-' . $menu_item->menu_item_parent,
                'title' => $menu_item->title,
                'href' => $menu_item->url,
                'meta' => array(
                    'title' => $menu_item->attr_title,
                    'target' => $menu_item->target,
                    'class' => implode( ' ', $menu_item->classes ),
                ),
            ) );
        }
    }