Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Add Menu Links to the New WordPress 3.3 Tool Bar

Posted on December 28, 2011 by Pippin in Intermediate, Quick Tips, Tutorials, WordPress Admin / Dashboard 5 Comments
Home» Tutorials » Intermediate » Add Menu Links to the New WordPress 3.3 Tool Bar
Tweet
Love It - 0

The new admin bar, now named the tool bar, in WordPress 3.3 is great. It’s really well organized and cleans the dashboard up a lot. It’s also great for developers because adding new links to settings pages, documentation, or other useful links, is quite simple. In this quick tip, I’m going to show you how to add new links to the tool bar.

To add new links to the tool bar, we’ll use the $wp_admin_bar global and the new add_menu() method available in 3.3. According to the codex, the add_menu() method may only be available when connected to the admin_bar_menu hook. Because of this, we will wrap our add_menu() instances inside of a function that is hooked to “admin_bar_menu”.

So to add a single link to our menu bar, we can do this:

1
2
3
4
5
6
7
8
9
10
11
12
add_action('admin_bar_menu', 'pippin_add_tool_bar_items', 100);
function pippin_add_tool_bar_items($admin_bar)
{
	$admin_bar->add_menu( array(
		'id'    => 'my-item',
		'title' => 'My Item',
		'href'  => 'http://google.com',
		'meta'  => array(
			'title' => __('My Item')
		),
	));
}

This will add a link called “My Item” that is linked to the Google home page.

If we want to add a sub menu item to our “My Item” link, then we can do this:

1
2
3
4
5
6
7
8
9
10
11
$admin_bar->add_menu( array(
	'id'    => 'my-sub-item',
	'parent' => 'my-item',
	'title' => 'My Sub Menu Item',
	'href'  => '#',
	'meta'  => array(
		'title' => __('My Sub Menu Item'),
		'target' => '_blank',
		'class' => 'my_menu_item_class'
	),
));

Note that the “parent” option is set equal to the “id” of our first menu item. So all together, with two sub menu links, it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
add_action('admin_bar_menu', 'pippin_add_tool_bar_items', 100);
function pippin_add_tool_bar_items($admin_bar)
{
	$admin_bar->add_menu( array(
		'id'    => 'my-item',
		'title' => 'My Item',
		'href'  => '#',
		'meta'  => array(
			'title' => __('My Item'),			
		),
	));
	$admin_bar->add_menu( array(
		'id'    => 'my-sub-item',
		'parent' => 'my-item',
		'title' => 'My Sub Menu Item',
		'href'  => '#',
		'meta'  => array(
			'title' => __('My Sub Menu Item'),
			'target' => '_blank',
			'class' => 'my_menu_item_class'
		),
	));
	$admin_bar->add_menu( array(
		'id'    => 'my-second-sub-item',
		'parent' => 'my-item',
		'title' => 'My Second Sub Menu Item',
		'href'  => '#',
		'meta'  => array(
			'title' => __('My Second Sub Menu Item'),
			'target' => '_blank',
			'class' => 'my_menu_item_class'
		),
	));
}

This will create a menu with this structure:

  • My Item
    • My Sub Menu Item
    • My Second Sub Menu Item
Tweet Follow @pippinsplugins
add_menu, admin_bar_menu, admin_menu, toolbar

5 comments on “Add Menu Links to the New WordPress 3.3 Tool Bar”

  1. Bruce says:
    December 28, 2011 at 6:32 pm

    Nicely written with a good flow, and a great tip!

    Reply
    • Pippin says:
      December 28, 2011 at 6:48 pm

      Thanks Bruce.

  2. ed says:
    December 29, 2011 at 10:02 am

    Thanks!

    Reply
  3. republic says:
    December 30, 2011 at 10:48 am

    Thanks for the write up, it’s really clear.

    How do we make a sub menu item have the grey background over the white though? For example the individual site listings under the My Sites menu.

    Reply
    • Pippin says:
      December 30, 2011 at 12:41 pm

      I’m not sure on that one, but I will look into it and update the post.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Writing Your First WordPress Plugin Part 5

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Submitting Your First Pull Request to a WordPress Plugin on Github (1)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

  • Plugin Development 101 – Intro to Loading Scripts and Styles (16)

    In this part of Plugin...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • Ride after the storm http://t.co/VXG1B8ONcs
    May 20, 2013
  • You may think you have a perfect theme / plugin, but even great ones cause conflicts sometimes
    May 19, 2013
  • Tip: help the developer out and be willing to at least try switching themes / plugins for a moment to debug
    May 19, 2013

Topics

Sugar Event Calendar the_content Rémi Corson get_user_meta wp_enqueue_script add_options_page attachments contextual help register_setting meta box featured add_shortcode hook login forms authors short codes attachment Related posts plugin do_action mail chimp image recent posts comments apply_filters post types bbpress short code taxonomies custom post type gallery Ajax images Stripe taxonomy jquery widgets users add_filter add_action easy content types widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2011 Pippin's Plugins