Add Menus to the Admin Bar of WordPress


The new WordPress admin bar facilitates the access of backend and frontend to areas of the back end. Of course, this new control element of WordPress is expandable and can be adapted to the needs of the user. In some areas and for various needs, quick access to different areas is certainly interesting.

The newly created menus can be adapted as usual rights to the appropriate roles. A small example will demonstrate the integration.

Within the Admin Bar you can represent hierarchies, which are then coupled with a default Admin Bar with a mouse over effect. In the following example, a search on the WordPress Codex is integrated in the Admin bar. There are still things to do in terms of CSS, but I think this example points out some possibilities and can be used for your own ideas and requirements.

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

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

    $codex_search = '
<form style="margin: 5px 0 0;" action="http://wordpress.org/search/do-search.php" method="get">
        <input class="adminbar-input" maxlength="100" name="search" size="13" type="text" value="' . __( 'Search the Codex', 'textdomain' ) . '" />
        <button 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 the Codex', 'textdomain' ), '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 );

The function add_menu() 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 => '' );

Comments are closed.

8 comments

  1. Latz

    Oh great, now every plugin will put a menu in the admin bar :-(

    Next stop: a plugin preventing others from cluttering the admin bar.

    latz

  2. JohnnyPea

    @Latz Good point! I hope plugin developers will be reasonable.

    Thanks for the tip. Please, what is that "micro" sign in your admin bar (on the left)?

  3. JohnnyPea

    I wanted to say "on the right", sorry.

  4. Frank

    @Latz: yes, you are right; but i wish this for all menus and boxes inside the core; maybe a core plugin. But i will this include later in my plugin Adminimize, with this plugin i creat all backends for customer.

  5. Frankie Jarrett

    Very handy. I was able to modify this function to add a link to some "Theme Options" in the Appearance menu. Thank you!


    // Add Theme Options to Admin Bar
    function theme_options_link() {
    global $wp_admin_bar, $wpdb;
    if ( !is_super_admin() || !is_admin_bar_showing() )
    return;
    $wp_admin_bar->add_menu( array( 'parent' => 'appearance', 'title' => __( 'Theme Options', 'urbanchurch' ), 'href' => get_bloginfo('url').'/wp-admin/themes.php?page=options.php' ) );
    }
    add_action('admin_bar_menu', 'theme_options_link', 1000);

    //Frankie

  6. Jonathan

    hi, im using wordpress multi-site how can I display that bar. thanks

  7. Michael

    @Jonathan: Its a feature of WordPress 3.1

One pingback

  1. Weblog Tools Collection: Adding Menus To The Admin Bar - Wordpress Creator