Make WordPress Core

Opened 12 months ago

Last modified 12 months ago

#40927 new defect (bug)

Passing a float as the position in add_menu_page can override other menu items

Reported by: justinbusa Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 4.8
Component: Administration Keywords: has-patch
Focuses: administration Cc:


If you pass a float value for the $position argument of add_menu_page, PHP will convert it to an integer which overrides any other menu items with that position.

Per the PHP docs...

Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.

You can see this happening with the following snippet. The Test 2 menu item will show but Test 1 will not.

add_action( 'admin_menu', function() {
        add_menu_page( 'Test 1', 'Test 1', 'manage_options', 'test-1', '', '', 64 );
        add_menu_page( 'Test 2', 'Test 2', 'manage_options', 'test-2', '', '', 64.99 );
} );

However, if you pass 64.99 in as a string instead of a float, Test 1 will show.

Patch attached, thanks!

Attachments (4)

plugin.diff (508 bytes) - added by justinbusa 12 months ago.
40927.diff (1.3 KB) - added by dd32 12 months ago.
40927.2.diff (513 bytes) - added by justinbusa 12 months ago.
40927.3.diff (539 bytes) - added by justinbusa 12 months ago.

Download all attachments as: .zip

Change History (13)

12 months ago

#1 @justinbusa
12 months ago

  • Keywords has-patch dev-feedback added

12 months ago

#2 @dd32
12 months ago

  • Component changed from Menus to Administration
  • Keywords dev-feedback removed

Previously: #23316

40927.diff is a bit more WordPress-like version of plugin.diff. I do wonder if we should just do something like this:

if ( ! is_int( $position ) && ! is_string( $position ) ) {
   $position = (string) $position;

#4 @justinbusa
12 months ago

@dd32 I agree as $position should always be either an int or a string. New patch attached.

12 months ago

#5 @justinbusa
12 months ago

Actually, I think that's going to break the if ( null === $position ) statement below it. New patch attached.

12 months ago

#6 follow-up: @dd32
12 months ago

Thanks for catching that! 40927.3.diff looks good to me.

I'll loop back onto this later to test it properly, and see if there's any unit testing opportunity here (not an easy one I don't think).

#7 in reply to: ↑ 6 @justinbusa
12 months ago

Great! Thanks for checking it out.

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.

12 months ago

#9 @SergeyBiryukov
12 months ago

  • Milestone changed from Awaiting Review to Future Release
Note: See TracTickets for help on using tickets.