Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I extend the functions of the WordPress 3.1 admin bar in my plugins?

I am looking for specific hooks and filters to use in adding links or other features to the admin bar.

A good example of what has already been done is the Yoast WordPress SEO (Wordpress Plugin).

alt text

Currently there is no documentation available on extending the admin bar. According to The Codex there are two filters to turn it off or not show it:

no_admin_bar() & show_admin_bar()

share|improve this question
3  
Can you not just take a peak at how the Yoast plugin does it, or is the code a little to abstract to make sense of? –  t31os Dec 7 '10 at 16:39
1  
Agreeing with t31os. It helps to research your question before asking... ;-) –  Denis de Bernardy Dec 7 '10 at 18:14
5  
Really? I think this is a good question. Pretty much every question on here can be answered by "doing some research". Of course I could dig through Yoast's code and find out how he did it but as it is right now no documentation on extending the admin bar exists. The Yoast WordPress SEO Plugin was just an example as I am sure that more can be done than just adding links. –  Chris_O Dec 7 '10 at 18:26
    
3.1 is in beta, it's less likely for documentation to exist for functionality that is subject to change. Don't get me wrong though, i'd be curious to see some examples myself in anticipation of 3.1... (i tend to use the stable branch as i spend more time supporting whatever the majority of users are on).... (or is this in 3.0.2?) –  t31os Dec 7 '10 at 18:41

3 Answers 3

up vote 16 down vote accepted

Yoast's plug-in is actually a very good example if all you want to do is add menus. Basically, the admin bar is just an alternate set of links to the same plug-in admin pages you have in the sidebar. To add the top-level SEO menu, Yoast does the following:

$wp_admin_bar->add_menu( array( 'id' => 'wpseo-menu', 'title' => __( 'SEO' ), 'href' => get_admin_url('admin.php?page=wpseo_dashboard'), ) );

This adds a menu named "wpseo-menu" to the admin bar and directs users to the plug-in's dashboard when they click the link. Child links are added in a similar fashion:

$wp_admin_bar->add_menu( array( 'parent' => 'wpseo-menu', 'id' => 'wpseo-kwresearch', 'title' => __( 'Keyword Research' ), '#', ) );

You just specify the "parent" of the menu you're adding.

Then you can go as deep as you need to, calling $wp_admin_bar->add_menu() when you need to and specifying the appropriate information.


For reference, the variable, $wp_admin_bar is an instance of the class WP_Admin_Bar() within WordPress. It has several different methods and properties, but the one you're most interested in here is, obviously, add_menu(). This method accepts certain 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 => '' );

But the rest of the WP_Admin_Bar() class is pluggable. It just depends on what exactly you're trying to do and how you want to do it.

See Also:

share|improve this answer
    
Very good answer. I just finished looking over the PHPXref. –  Chris_O Dec 7 '10 at 18:55

Download the nightly build, and check out these two files;

  • wp-includes/admin-bar.php
  • wp-includes/class-wp-admin-bar.php

The class WP_Admin_Bar is essentially the 'API', whilst the file admin-bar.php uses it to build the default bar and fire off a load of hooks.

function my_admin_bar()
{
    global $wp_admin_bar;
    $wp_admin_bar->add_menu(array(
        'parent' => 'my-account', // optional
        'id'     => 'my-unique-id',
        'title'  => '',
        'href'   => ''
    ));
}
add_action('admin_bar_menu', 'my_admin_bar');

That's pretty much the basics - this is all I've gathered from a quick gander myself (to be honest, it's a bit annoying the hook admin_bar_menu doesn't pass back the instance of WP_Admin_Bar - I hate all these globals!)

share|improve this answer
2  
That annoyance would be a good reason to open a ticket on Trac so we can patch it for WP 3.1.1 ... –  EAMann Dec 7 '10 at 18:56
2  
To followup: TheDeadMedic did open a ticket and it was included in time for 3.1, so you don't need to read the global variable, it will be passed by reference as the first parameter. –  Jan Fabry Apr 9 '11 at 20:04

small example, i had write this also on wp-hackers list before view days

function wp_codex_search_form() {
    global $wp_admin_bar, $wpdb;

    if ( !is_super_admin() || !is_admin_bar_showing() )
        return;

    $codex_search = '<form target="_blank" method="get" action="http://wordpress.org/search/do-search.php" style="margin:2px 0 0;">
        <input type="text" onblur="this.value=(this.value==\'\') ? \'Search the Codex\' : this.value;" onfocus="this.value=(this.value==\'Search the Codex\') ? \'\' : this.value;" maxlength="100" value="Search the Codex" name="search" class="adminbar-input">
        <button type="submit" class="adminbar-button">
            <span>Go</span>
        </button>
    </form>';

    /* Add the main siteadmin menu item */
    $wp_admin_bar->add_menu( array( 'id' => 'codex_search', 'title' => 'Search Codex', 'href' => FALSE ) );
    $wp_admin_bar->add_menu( array( 'parent' => 'codex_search', 'title' => $codex_search, 'href' => FALSE ) );
}
add_action( 'admin_bar_menu', 'wp_codex_search_form', 1000 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.