3

I would like to add a new menu item in the admin bar. So far, I have done the following:

function add_book_menu_item ($wp_admin_bar) {

    $args = array (
            'id'        => 'book',
            'title'     => 'Book',
            'href'      => 'http://example.com/',
            'parent'    => 'new-content'
    );

    $wp_admin_bar->add_node( $args );
}

add_action('admin_bar_menu', 'add_book_menu_item');

This is creating the Book menu item underneath the + New menu (in the admin toolbar). However, the Book item comes in first (it is before the Post menu item). I would like it to appear between the Media and Page items.

The following image shows what I would like to do.

enter image description here

How do I do that?

Thanks.

2

You could try using Jquery.

The new menu drop down --- wp-admin-bar-new-content ordering ---- li:eq(0)

 jQuery(document).ready(function ($) {
      // swaps 3rd and puts it before the 1st menu (note that orders start at 0)
  $("#wp-admin-bar-new-content li:eq(3)").before($("#wp-admin-bar-new-content li:eq(0)"));
});

new order would be (post,media,book,page) You would have to change this if new plugins add new menus but it will work for you until you look into the below.

This page from the wordpress https://codex.wordpress.org/Function_Reference/get_nodes shows how to build a new menu drop down containing all menus in the admin bar and possibly could be edited to recreate the changes via a wordpress hook letting you rebuild the 'new-content' to suit your needs with more control.

  • Thanks. I tried the jQuery solution as well. It works, but has two disadvantages. First, it uses jQuery, which is not the cleanest approach to solving the issue (I was hoping for a PHP only solution). Second, if there are other menu items as you specified, then the order will not be right. – Greeso Mar 23 '17 at 21:05
1

every menu have an number . lower the number priority.

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

more clear idea you can check the link below

http://natko.com/custom-menu-item-position-in-wordpress-admin-bar-toolbar/

  • Thanks. I tried this already. Does not work for what I am doing. This would work for the root menu items. For me, everything is under the New node (which is a root node). Therefore, priorities would not work. By the way, if I add priority of 70, then Book appears on top. If I make the priority 71, it is at the bottom of the list (beneath User). – Greeso Mar 23 '17 at 17:57
  • natko.com/… Has a list of the items numbers for arranging order. – Sam Mar 23 '17 at 18:00
  • @Sam - Yes, but the root node is a custom node (namely, it is Custom Node), and his solution would not work on extending a node that is already created (i.e., the New node that I like to extend). – Greeso Mar 23 '17 at 18:10
  • can you set 20 or 30 ? – Akankha Ahmed Mar 23 '17 at 18:23
  • @Akankhaahmed - Yes, I did, anything below 70 shows the item at the top. – Greeso Mar 23 '17 at 18:24

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.