Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#27735 closed defect (bug) (fixed)

unregister_nav_menu leaves menu visible after it has been set

Reported by: mbootsman's profile mbootsman Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.0 Priority: normal
Severity: normal Version: 3.8.1
Component: Menus Keywords: has-patch
Focuses: administration Cc:


When setting a menu on the primary or secondary locations the menu's are displayed according to the theme specs. When removing the menu location with unregister_nav_menu, the menu is still displayed on the front, but cannot be edited in the back-end, since we just unregistered it.

Desired behavior would be: Do not display an unregistered menu location, even if it has been set with a menu previously.

Attachments (2)

27735.patch (532 bytes) - added by SergeyBiryukov 10 years ago.
27735.2.patch (549 bytes) - added by SergeyBiryukov 10 years ago.

Download all attachments as: .zip

Change History (12)

#1 @nacin
10 years ago

Hi mbootsman, thanks for the report. register_nav_menu() and unregister_nav_menu() actually is for registering "locations", despite the slightly more generic name. If unregister_nav_menu() isn't removing the menu, then it sounds like one of two things:

  • The call to wp_nav_menu() on the front-end is wrong and is allowing the second menu to be displayed, such as by specifically specifying a menu or by not specifying a theme location.
  • unregister_nav_menu() is not being called on the frontend.

#2 @mbootsman
10 years ago

Andrew, thanks for your reply.
I realize the naming is a bit confusing, but I get that ;)

Since I am using Genesis, and added the unregister_nav_menu to the init hook, I have tested this with twentyfourteen too to filter out theme-specific code. And it gives the same result.

Let me clarify by the following steps, using the twentyfourteen theme:

  • Create a menu
  • Assign menu to both primary and secondary locations via Appearance - Menus
  • View frontpage, this will show the same menu on the primary/secondary location
  • Edit functions.php and add unregister_nav_menu( 'secondary' );
  • Refresh frontpage, this will show both menu locations filled with the created menu
  • Refresh the Appearance - Menus locations tab, this only shows Primary location

I don't know if this a bug or as-designed, but I just doesn't feel right.

#3 @mbootsman
10 years ago

Just a little reminder on this. Hope I explained the steps clear enough ;)

#4 @SergeyBiryukov
10 years ago

  • Keywords has-patch added

Confirmed. unregister_nav_menu() has no effect on has_nav_menu() or wp_nav_menu().

has_nav_menu() description says: "Whether a registered nav menu location has a menu assigned to it", but it doesn't actually check if the location is registered: tags/3.9.1/src/wp-includes/nav-menu.php#L125.

27735.patch fixes that. Perhaps the same check should be added to wp_nav_menu() where it checks for a menu based on theme_location: tags/3.9.1/src/wp-includes/nav-menu-template.php#L276.

#5 @mbootsman
10 years ago

Thanks for the patch Sergey!

#6 @SergeyBiryukov
10 years ago

  • Milestone changed from Awaiting Review to 4.0

#7 @wonderboymusic
10 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 28836:

return false in has_nav_menu() if the $location does not exist in the $_wp_registered_nav_menus global.

Props SergeyBiryukov.
Fixes #27735.

#8 @nacin
10 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

[28836] can actually use get_registered_nav_menus() here.

#9 @SergeyBiryukov
10 years ago

27735.2.patch uses get_registered_nav_menus().

#10 @nacin
10 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

There are a bunch of other direct uses of $_wp_registered_nav_menus. Closing out, opened #29461.

Note: See TracTickets for help on using tickets.