Make WordPress Core

Opened 23 months ago

Last modified 23 months ago

#56491 new defect (bug)

unregister_nav_menu() does not update theme mod 'nav_menu_locations'

Reported by: studiolxv's profile studiolxv Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Menus Keywords: has-patch
Focuses: Cc:

Description

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
// 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
// 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;
}

Attachments (1)

nav-menus.php (45.7 KB) - added by studiolxv 23 months ago.
Lines 109 - 137 contains the updated function

Download all attachments as: .zip

Change History (2)

@studiolxv
23 months ago

Lines 109 - 137 contains the updated function

#1 @studiolxv
23 months ago

Excuse me this is wp-includes/nav-menu.php not wp-admin/nav-menus.php!

Note: See TracTickets for help on using tickets.