[wp-trac] [WordPress Trac] #56491: unregister_nav_menu() does not update theme mod 'nav_menu_locations'
WordPress Trac
noreply at wordpress.org
Thu Sep 1 22:48:45 UTC 2022
#56491: unregister_nav_menu() does not update theme mod 'nav_menu_locations'
--------------------------+-----------------------------
Reporter: studiolxv | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Menus | Version:
Severity: normal | Keywords: has-patch
Focuses: |
--------------------------+-----------------------------
When you register a menu location then attach a wp_nav menu to a that
location it is added to array **''global'' $_wp_registered_nav_menus** and
then another array stored in **get_theme_mod('''nav_menu_locations''')**.
{{{#!php
<?php
// wp-admin/nav-menus.php Line 65
set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_REQUEST['menu-
locations'] ) )
}}}
If you change the menu location name a few times and associate a wp_nav
menu to the new location, **get_theme_mod('''nav_menu_locations''')**
array will keep appending the new data and the older locations are still
in this theme_mod array.
Currently **unregister_nav_menu()** only removes the attached menu ID from
**''global'' $_wp_registered_nav_menus**. It does not remove this menu
location key=>value pair from the
**get_theme_mod('''nav_menu_locations''')** array.
Proposed Patch & update to unregister_nav_menu() in wp-admin/nav-menus.php
on Line 109
{{{#!php
<?php
// wp-admin/nav-menus.php - Starting Line 109
/**
* Unregisters a navigation menu location for a theme and updates theme
mod 'nav_menu_locations'
*
* @since 6.1.1
*
* @global array $_wp_registered_nav_menus
*
* @param string $location The menu location identifier.
* @return bool True on success, false on failure.
*/
function unregister_nav_menu($location)
{
global $_wp_registered_nav_menus;
if (is_array($_wp_registered_nav_menus) &&
isset($_wp_registered_nav_menus[$location])) {
unset($_wp_registered_nav_menus[$location]);
set_theme_mod('nav_menu_locations',
$_wp_registered_nav_menus);
if (empty($_wp_registered_nav_menus)) {
_remove_theme_support('menus');
}
}
$theme_locations = get_theme_mod('nav_menu_locations');
if (is_array($theme_locations) && array_key_exists($location,
$theme_locations)) {
unset($theme_locations[$location]);
set_theme_mod('nav_menu_locations', $theme_locations);
return true;
}
return false;
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/56491>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list