How can I specify the position of an admin bar item added with $wp_admin_bar->add_menu() or add_node()?

I want to add a link to my site in the admin bar, and make that link the leftmost item in the admin bar. I can add a link with this in a plugin function:

$wp_admin_bar-add_menu( array(
        'id' = 'my-link',
        'title' = __('MySite'),
        'href' = site_url() 
    ) );

But I'd like to make it the leftmost link in the admin bar, i.e. all the way in the top left corner. Is there a way to do this?

Topic admin-bar Wordpress

Category Web


I attempted Charles solution but with no success. I did find out though that you can specify whether which side of the admin bar your content is added.

Using

add_action( 'admin_bar_menu', 'yourfunctionname' );

will add your new content to the left side of the admin bar before the default content.

Using

add_action( 'wp_before_admin_bar_render', 'yourfunctionname' );

will add your new content to the right side of the admin bar after the default content.


If you want to completely reorganize the admin bar you must use WP_Admin_Bar().

Example:

function reorder_admin_bar() {
    global $wp_admin_bar;

    // The desired order of identifiers (items)
    $IDs_sequence = array(
        'wp-logo',
        'site-name',
        'new-content',
        'edit'
    );

    // Get an array of all the toolbar items on the current
    // page
    $nodes = $wp_admin_bar->get_nodes();

    // Perform recognized identifiers
    foreach ( $IDs_sequence as $id ) {
        if ( ! isset($nodes[$id]) ) continue;

        // This will cause the identifier to act as the last
        // menu item
        $wp_admin_bar->remove_node($id);
        $wp_admin_bar->add_node($nodes[$id]);

        // Remove the identifier from the list of nodes
        unset($nodes[$id]);
    }

    // Unknown identifiers will be moved to appear after known
    // identifiers
    foreach ( $nodes as $id => &$obj ) {
        // There is no need to organize unknown children
        // identifiers (sub items)
        if ( ! empty($obj->parent) ) continue;

        // This will cause the identifier to act as the last
        // menu item
        $wp_admin_bar->remove_node($id);
        $wp_admin_bar->add_node($obj);
    }
}
add_action( 'wp_before_admin_bar_render', 'reorder_admin_bar');

https://codex.wordpress.org/Class_Reference/WP_Admin_Bar


If I am correct these are the default positions:

  • wp_admin_bar_wp_menu - 10
  • wp_admin_bar_my_sites_menu - 20
  • wp_admin_bar_site_menu - 30
  • wp_admin_bar_updates_menu - 40
  • wp_admin_bar_comments_menu - 60
  • wp_admin_bar_new_content_menu - 70
  • wp_admin_bar_edit_menu - 80

small code snippet from what I use:
add_action('admin_bar_menu', 'your_function_name', 10);

The 10 should bring it to the most left side in the adminbar.

At moment we are at WP version 3.8 and it still work like a charm.

Added example:

function add_item($admin_bar)  {
$args = array(
    'id'        => 'your-link', // Must be a unique name
    'title'     => 'Yoursite', // Label for this item
    'href'      =>__ ('your_site_url'),
    'meta'  => array(
        'target'=> '_blank', // Opens the link with a new tab
        'title' => __('Yoursite'), // Text will be shown on hovering
    ),
);
$admin_bar->add_menu( $args);
}
add_action('admin_bar_menu', 'add_item', 10); // 10 = Position on the admin bar

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.