[wp-trac] [WordPress Trac] #51869: float values for menu positions overwriting each other

WordPress Trac noreply at wordpress.org
Tue Nov 24 19:13:45 UTC 2020


#51869: float values for menu positions overwriting each other
----------------------------+-----------------------------
 Reporter:  pomegranate     |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  Menus           |    Version:  5.6
 Severity:  normal          |   Keywords:
  Focuses:  administration  |
----------------------------+-----------------------------
 When using `add_menu_page()` and setting the `$position` argument with a
 float (say, `57.77`) this will overwrite other menu items with either `57`
 (int) for $position or `57.#` (any float). All of them end up at the
 `57`(int) key. This is despite the check for duplicates. Using `'57.7'`
 (string) instead will not have this issue.

 https://www.php.net/manual/en/language.types.array.php
 >  Floats are also cast to ints, which means that the fractional part will
 be truncated. E.g. the key 8.7 will actually be stored under 8.

 The effect is that menu items disappear/get overwritten.

 Here's a simple example to demonstrate the bug:
 {{{#!php
 add_action( 'admin_menu', function(){
         add_menu_page(
                 __( 'Heinz Ketchup', 'heinz-ketchup' ),
                 __( 'Heinz Ketchup', 'heinz-ketchup' ),
                 'manage_options',
                 'heinz',
                 '__return_empty_string',
                 'dashicons-admin-users',
                 57
         );
         add_menu_page(
                 __( 'Home Made Ketchup', 'home-made-ketchup' ),
                 __( 'Home Made Ketchup', 'home-made-ketchup' ),
                 'manage_options',
                 'homemade',
                 '__return_empty_string',
                 'dashicons-admin-users',
                 57.77
         );
 } );

 }}}

 Only the second menu item will be shown. I believe this is undesired
 behavior.
 The solution would be to cast any `float` value to a string before setting
 it as a key in the menu array.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/51869>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list