[wp-trac] [WordPress Trac] #19380: There is no way to specify placement of a new menu item in the Admin Bar

WordPress Trac wp-trac at lists.automattic.com
Mon Nov 28 15:30:37 UTC 2011


#19380: There is no way to specify placement of a new menu item in the Admin Bar
--------------------------+------------------------------
 Reporter:  willshouse    |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Admin Bar     |     Version:  3.3
 Severity:  minor         |  Resolution:
 Keywords:                |
--------------------------+------------------------------
Description changed by scribu:

Old description:

> I recently opened ticket 19371 (
> http://core.trac.wordpress.org/ticket/19371 ) and @Scribu has added a
> fantastic and simple solution to be able to modify existing menu items.
> The only problem, however, is that it is impossible to control placement
> of a new menu item.
>
> For example, if I wanted to add a class on the "wp-logo" menu item, I
> would use the method specified in 19371 to remove it, modify it, and add
> it back.
>
> {{{
>
> add_action( 'admin_bar_menu', function( $admin_bar ) {
>         $node = $admin_bar->remove_menu( 'wp-logo' );
>
>         $node['meta']['class'] = 'foo';
>
>         $admin_bar->add_menu( $node );
> }, 99 );
>
> }}}
>

> The only problem is that it would put the menu item at the far right of
> the other menu items. I could then remove and re-add all the other menu
> items. However I don't think there is a way to get a list of all the top
> level menu items from within the admin bar class, so you would have to
> know the ID of each one from a print_r debug of the object and hard code
> the items in. This would break if a plugin added a menu item that wasn't
> in the hard coded list.
>
> My thoughts would be either add a parameter to the add_menu() function
> such as priority, which could be boolean and cause the item to be added
> with array_unshift() instead of the current method, or a parameter that
> would be be for the id to "add before menu item x" , or just a method
> that would return a list of ids of the root menu items.
>
> ps: I don't really need to add a CSS class to the wp-logo menu item, but
> this is the far-left-most item and I thought it would make the best
> example.

New description:

 I recently opened ticket #19371 and scribu has added a fantastic and
 simple solution to be able to modify existing menu items. The only
 problem, however, is that it is impossible to control placement of a new
 menu item.

 For example, if I wanted to add a class on the "wp-logo" menu item, I
 would use the method specified in #19371 to remove it, modify it, and add
 it back.

 {{{

 add_action( 'admin_bar_menu', function( $admin_bar ) {
         $node = $admin_bar->remove_menu( 'wp-logo' );

         $node['meta']['class'] = 'foo';

         $admin_bar->add_menu( $node );
 }, 99 );

 }}}


 The only problem is that it would put the menu item at the far right of
 the other menu items. I could then remove and re-add all the other menu
 items. However I don't think there is a way to get a list of all the top
 level menu items from within the admin bar class, so you would have to
 know the ID of each one from a print_r debug of the object and hard code
 the items in. This would break if a plugin added a menu item that wasn't
 in the hard coded list.

 My thoughts would be either add a parameter to the add_menu() function
 such as priority, which could be boolean and cause the item to be added
 with array_unshift() instead of the current method, or a parameter that
 would be be for the id to "add before menu item x" , or just a method that
 would return a list of ids of the root menu items.

 ps: I don't really need to add a CSS class to the wp-logo menu item, but
 this is the far-left-most item and I thought it would make the best
 example.

--

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/19380#comment:3>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list