Add a Submenu to the WordPress Admin Bar

By  on  

A while back I wrote Add Menu Items to the WordPress Admin Menu, a post detailing how you can add items to the left side, existing "posts" menu bar.  Adding menu items has served me well but it's not as customized as I would like -- I'm still adapting to WordPress instead of WordPress adapting to me.  By creating a custom top bar menu, I can mix and match links all I want!

Like adding functionality to your theme and other admin area, the directives will go in your theme's functions.php file.  The code itself should be self explanatory:

function create_dwb_menu() {
	global $wp_admin_bar;

	$menu_id = 'dwb';
	$wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => __('DWB'), 'href' => '/'));
	$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Homepage'), 'id' => 'dwb-home', 'href' => '/', 'meta' => array('target' => '_blank')));
	$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Drafts'), 'id' => 'dwb-drafts', 'href' => 'edit.php?post_status=draft&post_type=post'));
	$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => __('Pending Comments'), 'id' => 'dwb-pending', 'href' => 'edit-comments.php?comment_status=moderated'));
}
add_action('admin_bar_menu', 'create_dwb_menu', 2000);

Setting an id on the parent menu item allows you to use the parent key for submenu items; the rest of the keys are easy to figure out.  With the menu created, you simply need to add the WordPress hook and specificity to add it!

Recent Features

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    Create a Photo Stack Effect with Pure CSS Animations or MooTools

    My favorite technological piece of Google Plus is its image upload and display handling.  You can drag the images from your OS right into a browser's DIV element, the images upload right before your eyes, and the albums page displays a sexy photo deck animation...

  • By
    Morphing Elements Using MooTools and CSS

    Morphing an element between CSS classes is another great trick the MooTools JavaScript library enables you to do. Morphing isn't the most practical use of MooTools, but it's still a trick at your disposal. Step 1: The XHTML The block of content that will change is...

Discussion

  1. I you want to remove a menu item , here the code.

    add_action('admin_bar_menu','ank_remove_admin_bar_menu', 99);
    
    function ank_remove_admin_bar_menu($wp_admin_bar){
        $wp_admin_bar->remove_menu('wp-logo');
    }
    
  2. Isuru

    Thank you Mr. Walsh! I needed this. Awesome!

  3. This is super easy!though I wish they had some kind of visual interface to do this, as it is there is Joomla and Drupal.

  4. Gavin

    What would be great would be a WP plugin that uses the built-in WordPress “Links” (http://codex.wordpress.org/Template_Tags/wp_list_bookmarks) and creates WP Admin Bar menus for each link category, and then adds the links from each category to the menu.

    Seems like a nice way to leverage the built-in WP Links feature for something that would actually be useful.

    Shouldn’t be too hard to do …. I might have a go when I get time.

    Thanks for the article.

  5. Hasse Andre

    Hi.

    Thanks for this.

    Is there a way to open the URL in a new window?
    Here is my code:

    function create_dwb_menu() {
    	global $wp_admin_bar;
    
    	$menu_id = 'dwb';
    	$wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => __('NEW MENU ITEM'), 'href' => 'http://website.com/'));
    }
    add_action('admin_bar_menu', 'create_dwb_menu', 2000);
    
  6. To have this open in a new window, add in the meta option, like this: 'meta' => array( 'target' => '_blank' )

  7. iLen

    Thank you. I much use this code for my next plugin :)

  8. Ovidiu

    Thank you, this works great, even after all those years.

    However, what I would need is a way to add this on the right, to the “top-secondary” adminbar menu, next to the user actions. I’ve managed to add it using this function:

    function get_support_link($wp_admin_bar) {
       $args = array(
          'id' => 'get-support',
          'title' => 'Need Help?', 
          'href' => '/wp-admin/get-support.php', 
          'parent' => 'top-secondary', 
          'meta' => array(
             'class' => 'get-suport', 
             'title' => 'Use the contact form to contact Support',
             'target' => '_blank',
          )
       );
       $wp_admin_bar->add_node($args);
    }
    add_action('admin_bar_menu', 'get_support_link', 99);
    

    the ‘parent’ => ‘top-secondary’ being the key here. Yes, I could use the same function to add submenu items to this one, but I need to add about 5 items (maybe more) and adding same function again and again looks awful. I feel that this can be done cleaner, like your function here, but I can’t figure out how to add this function to the ‘top-secondary’ adminbar location. Any ideas?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!