[wp-trac] [WordPress Trac] #45361: Integer menu slugs should not be supported

WordPress Trac noreply at wordpress.org
Thu Nov 15 20:18:07 UTC 2018


#45361: Integer menu slugs should not be supported
--------------------------+----------------------------
 Reporter:  desrosj       |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Future Release
Component:  Menus         |    Version:
 Severity:  normal        |   Keywords:  needs-patch
  Focuses:                |
--------------------------+----------------------------
 Currently, menus can be registered with an integer slug. This can cause
 unintended issues that are difficult to debug.

 Consider the following snippet:

 {{{
 register_nav_menus( array(
         'primary' => 'Primary',
         1         => 'First',
         2         => 'Second',
 ) );
 }}}

 The assumption is that the resulting list of menus would match what was
 passed. Instead, the result is this:

 {{{
 array(
     'primary' => 'Primary',
     0         => 'First',
     1         => 'Second',
 )
 }}}

 This would cause `wp_nav_menu( array( 'theme_location' => 1 ) )` to return
 the wrong menu.

 This is happening because `array_merge()` is used to add new nav menu
 locations to the global `$_wp_registered_nav_menus` variable.
 `array_merge()` renumbers numeric indexes, incrementing keys starting from
 zero in the result array.

 My proposal is to add a `_doing_it_wrong()` warning when registering a nav
 menu with a numeric index. This will at least inform developers of the
 potential issue, and would also encourage better practices of using a
 string slug for nav menus.

 Casting the integer as a string could also be explored, that is likely to
 be complicated for backward compatibility.

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


More information about the wp-trac mailing list