Is the new WordPress 3.1 admin bar pluggable and how can I extend it?

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).

Read More

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()

Related posts

Leave a Reply

3 comments

  1. 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:

  2. 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 );
    
  3. 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!)