5

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.

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

4
  • See this question for further reference on adding a menu to the WordPress admin bar.
    – Chris_O
    Apr 20, 2011 at 6:43
  • Interesting question. Currently this is not directly possible, but it should be doable, probably with a custom Walker that does not output HTML but attaches it to the menu instead.
    – Jan Fabry
    Apr 20, 2011 at 7:26
  • @Chris_O Actually that is what I was trying to avoid. I do appreciate the hint though
    – shawn
    Apr 20, 2011 at 7:43
  • @Jan --Would love to hear more about the concept. Doing a search now to learn more about custom walkers, new area for me. Hoping people see the value in attaching actual menu's to the admin bar.
    – shawn
    Apr 20, 2011 at 7:45

1 Answer 1

5

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 ),
            ),
        ) );
    }
}
4
  • Wow that does indeed look like exactly what I was after. It's 3am so to late to implement tonight, will report back tomorrow, but did want to say thank you.
    – shawn
    Apr 20, 2011 at 9:35
  • This is actually working great, but I do need to make one change, which so far eludes me. The very top level navigation title is linking to the contact-us page yet there is no contact us page in the menu I assigned to the bar. All of the sub-menu-links do point to the proper pages though, so that is good. I would like to remove the link completely from the 'parent' navigation link.
    – shawn
    Apr 20, 2011 at 17:49
  • @shawn: The top menu item, labeled WPSE 15186 menu in my example? Weird, it should have an empty href attribute, and thus reload the current page when you click on it. You can also set the onclick attribute to return false; to make it do nothing.
    – Jan Fabry
    Apr 20, 2011 at 20:02
  • Yeah, that was really weird having it pull the contact page. Setting href='' solved the issue though. Thank you
    – shawn
    Apr 21, 2011 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.