Jan
04
2011

WordPress 3.1: The New Admin Bar

by   |  Posted in Tutorials  |  8 comments

I have been playing around with the latest release candidate for WordPress 3.1 and some of the new features are pretty cool. I really like the admin bar and I spent a bit of time yesterday figuring out how I could take advantage of it for my themes. All I really wanted to do was add my theme options pages so that they can be easily accessible. Luckily, there is a new action I could hook into called admin_bar_menu().

Admin Bar

First we need to add an action to our function.php file.

add_action( 'admin_bar_menu', 'add_menu_admin_bar' ,  70);

The 70 at the end indicates where the new menu will appear. If you want it to appear first on the left, you can leave that parameter blank or set it to 10. The number needs to be in increments of ten to set where the menu will appear. 70 will make it appear after the Appearance menu items.

Now we need to create our function which will use the global variable $wp_admin_bar to add the new menu items to the admin bar.

function add_menu_admin_bar() {
    global $wp_admin_bar;
 
    if ( !is_super_admin() || !is_admin_bar_showing() )
        return;
 
    $wp_admin_bar->add_menu( array( 'id' => 'theme_options', 'title' =>__( 'Magazine Basic', 'magazine-basic' ), 'href' => admin_url('admin.php')."?page=magazine-basic" ) );
    $wp_admin_bar->add_menu( array( 'parent' => 'theme_options', 'title' => __( 'Layout Options', 'magazine-basic' ), 'href' => admin_url('admin.php')."?page=magazine-basic" ) );
}

The add_menu() function accepts the following parameters:

title
default false
href
default false
parent
default false – pass the ID value for a submenu of that menu
id
defaults to a sanitized title value
meta
default false – array of any of the following options: array( ‘html’ => ”, ‘class’ => ”, ‘onclick’ => ”, target => ” );

Putting it all together into the function.php file would look like this.

function add_menu_admin_bar() {
    global $wp_admin_bar;
 
    if ( !is_super_admin() || !is_admin_bar_showing() )
        return;
 
    $wp_admin_bar->add_menu( array( 'id' => 'theme_options', 'title' =>__( 'Magazine Basic', 'magazine-basic' ), 'href' => admin_url('admin.php')."?page=magazine-basic" ) );
    $wp_admin_bar->add_menu( array( 'parent' => 'theme_options', 'title' => __( 'Layout Options', 'magazine-basic' ), 'href' => admin_url('admin.php')."?page=magazine-basic" ) );
}
add_action( 'admin_bar_menu', 'add_menu_admin_bar' ,  70);

Just make sure it is between the PHP tags.

I’ll be messing around with WP 3.1 for the rest of the week and posting about my discoveries so keep checking in.

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
Share the love...

Tags: , , ,

Short URL: http://bit.ly/hRsxFE

Discussion 8 Comments

  1. kevin on January 5, 2011 at 7:40 am

    Very interesting, any idea about the release date for this 3.1 version?

    • c.bavota on January 5, 2011 at 9:30 am

      Should be real soon.

  2. Lyndsy Simon on January 6, 2011 at 5:49 pm

    Wasn’t even aware of this. I’m looking forward to upgrading now :)

  3. Chris on February 23, 2011 at 3:13 pm

    Just upgraded and am playing with the above code. Thanks a bunch for the post, works like a charm!!

    Any idea how to get the id’s of the existing menu items so I can remove them?

  4. Carcinomatype.com on February 26, 2011 at 12:34 pm

    I think removing admin bar is better explained @ http://www.nichewp.com/how-to-remove-wordpress-3-1-admin-bar.html

    • c.bavota on February 26, 2011 at 12:52 pm

      I am not explaining how to remove it. The tutorial in on how to add items to the new admin bar.

  5. Alex on March 2, 2011 at 3:49 pm

    Quick note about this code snippet. Important to replace “exit” with “return” Exit will halt the script in functions.php and not load up other important stuff for non-super-admin.

  6. Detroit Clubs on March 4, 2011 at 12:31 am

    Good Stuff here. I will have to keep checking back for more information and recommend this site to some of friends and business partners.