Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#51869 closed defect (bug) (duplicate)

float values for menu positions overwriting each other

Reported by: pomegranate's profile pomegranate Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.8
Component: Administration Keywords:
Focuses: administration Cc:

Description

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:

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.

Change History (2)

#1 @SergeyBiryukov
4 years ago

  • Component changed from Menus to Administration
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi there, welcome to WordPress Trac! Thanks for the report.

Just noting that I was able to reproduce the issue. Looks like we're already tracking it in #40927.

Moving to the Administration component, as Menus is specifically for the Menus screen and nav menu functions.

#2 @hellofromTonya
4 years ago

  • Version changed from 5.6 to 4.8

Changing the Version to match version reported #40927.

Note: See TracTickets for help on using tickets.