WP 3.3 How to Add Menu Items to the Admin Bar?

The action hook admin_bar_menu does the job. Here’s what the comment in the core file says:

It includes the action “admin_bar_menu” which should be used to hook
in and add new menus to the admin bar. That way you can be sure that
you are adding at most optimal point, right before the admin bar is
rendered. This also gives you access to the $post global, among
others.

Usage:

add_action('admin_bar_menu', 'add_items');

function add_items($admin_bar)
{
//echo "<pre>";
//print_r($admin_bar);
//echo "<pre>";

$admin_bar->add_menu( array(
    'id'    => 'my-item',
            'parent' => 'top-secondary',
    'title' => 'My Item',
    'href'  => '#',
    'meta'  => array(
        'title' => __('My Item'),
    ),
) );
}

You’re gonna have to tweak a bit with it, because this code added the new item to the extreme left! I think examining the $admin_bar variable should give you an idea how to add an item.

Update: 'parent' => 'top-secondary' adds the item on the left of ‘Howdy…’! So I bet there are other such position values. I’m looking for them 😉

Update 2: 'parent' => 'new-content' adds the item in the New > Post, Media etc menu.

Update 3: 'parent' => 'wp-logo-external' adds the item in the menu popped out from the WordPress logo on the extreme left.

UPDATE:

I have written a blog post with all possible positions for the new item in the admin bar menu… If anyone interested please read here.

Leave a Comment