One of the cool features of WordPress 3.1 is the Admin menu bar. This is just like the bar at WordPress.com, for your self hosted site. Like just about every other part of WordPress, you can customize it with menus for your clients and/or users.

I first realized there was a hook for these menus during 3.1 beta testing, when I installed Joost de Valk’s WordPress SEO Plugin. He’s added some links to his settings pages and keyword research tools like Adwords. It’s easy to think of ways this could be used to provide an even more customized admin experience.

How do I add an admin bar menu?

Adding a custom menu to the admin bar is a lot like adding a menu to the admin sidebar. It’s pretty simple hook, with some of the same arguments. We can make them multiple levels, and give them a menu order.

There are generally 2 places to add functions and customize WordPress. A plugin file, or theme functions file.

First, we’ll add the action hook to use the function we will write. There are 3 arguments.

  1. Action hook name
  2. Function name ( that we will write )
  3. Menu order.
add_action('admin_bar_menu', 'my_admin_bar_menu', 99);

Now lets create the function we just used, and declare the global variable that will allow us access to the admin bar menu API, ‘wp_admin_bar‘.

function my_admin_bar_menu()  {
global $wp_admin_bar;

Now we will use the admin bar variable to add the menu, and tell WordPress about it using a few arguments. You don’t need all these arguments, but you need at least the id, and title arguments to create a menu item.

  • id – The unique identifier for this menu item.
  • title – The text that will display.
  • href – Menu link destination,
  • parent – Menu item to display link below.
  • meta – Link meta information like title, events, etc.

Here is an example of an admin link to the 37Signals Launchpad, with Basecamp and Campfire in the submenu. Of course, I would normally use links to the actual account subdomain pages. These can be easily modified to any other link.

WordPress Admin Bar Menu Example

by Fork it on Github
WordPress Admin Bar Menu Example
wordpress_admin_bar_menu.php
add_action('admin_bar_menu', 'webmaster_admin_bar_menu', 99);
function webmaster_admin_bar_menu() {

   global $wp_admin_bar;

   $menus[] = array(
      'id' => 'webmaster',
      'title' => 'Web Master',
      'href' => 'http://yourdomain.com/',
      'meta' => array(
         'target' => 'blank'
      )
   );

   $menus[] = array(
      'id' => 'basecamp',
      'parent' => 'webmaster',
      'title' => 'Basecamp',
      'href' => 'http://youraccount.basecamphq.com/',
      'meta' => array(
         'target' => 'blank'
      )
   );

   $menus[] = array(
      'id' => 'email',
      'parent' => 'webmaster',
      'title' => 'Email Jeff',
      'href' => 'mailto:you@yourdomain.com',
      'meta' => array(
         'target' => 'blank'
      )
   );

   foreach ( apply_filters( 'render_webmaster_menu', $menus ) as $menu )
       $wp_admin_bar->add_menu( $menu );

}

So now you can take one more step to further customize your whitelabeled administration areas for clients, or add a menu in your plugin. Just remember to use menus appropriately with plugins you distribute. Put them where users will naturally look for them. I would save the admin bar for actions used as often as post or network functions.