WordPress 3.1: The New Admin Bar

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.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie