Attach a navigation menu to the admin bar

I stumbled into this slick little snippet idea by Jan Fabry while visiting stack exchange. Adding this snippet to the functions.php of your wordpress theme will use the wordpress menu feature to create a new menu within the admin bar. Call your menu "WPSE 15186 test menu" or change the menu name within the code to whatever you like.

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 ),
        ) );

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!