0

I have a plugin that adds custom admin bar items, but I'd like them to appear all the way to the left. I know I can add and remove menu items with $wp_admin_bar->add_menu() etc, but how can I reorder them?

Edit: It looks like the admin bar menu item orders are defined in wp-includes/class-wp-admin-bar.php, but I can't figure out how to change this order from within my plugin function.

            add_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 );
            add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
            add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
            add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
2

This is an old question, but for others in the future the answer is.

You can add the following code to functions.php.

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_menu($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_menu($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

-2
function custom_menu_order($menu_ord) {  
    if (!$menu_ord) return true;  

    return array(  
        'index.php', // Dashboard  
        'separator1', // First separator  
        'edit.php', // Posts  
        'upload.php', // Media  
        'link-manager.php', // Links  
        'edit.php?post_type=page', // Pages  
        'edit-comments.php', // Comments  
        'separator2', // Second separator  
        'themes.php', // Appearance  
        'plugins.php', // Plugins  
        'users.php', // Users  
        'tools.php', // Tools  
        'options-general.php', // Settings  
        'separator-last', // Last separator  
    );  
}  
add_filter('custom_menu_order', 'custom_menu_order'); // Activate custom_menu_order  
add_filter('menu_order', 'custom_menu_order');  
  • It would add value to the solution if you present it with some explanations. – Maruti Mohanty Dec 9 '13 at 20:44
  • I tried echoing $menu_ord, but it doesn't return anything, so it might be empty. Also, my admin bar items are links with IDs, created with $wp_admin_bar->add_menu(), so I'm not sure I can reference them with a .php entry. – Jonathan Dec 10 '13 at 15:12
  • Does it work for any items starting with admin.php like admin.php?page=custom_settings_page? – Chittaranjan Dec 12 '13 at 13:19
  • 1
    Not helpful. This answer is about the admin menu. The question is about the Admin Bar. Different things. – Evan Mattson Sep 23 '14 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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