WordPress.org

Make WordPress Core

Opened 11 months ago

Closed 10 months ago

Last modified 10 months ago

#38612 closed defect (bug) (fixed)

Infinite refresh loop in Customizer when using the filter 'theme_mod_nav_menu_locations'

Reported by: Chouby Owned by: westonruter
Milestone: 4.7 Priority: normal
Severity: normal Version: 4.7
Component: Customize Keywords: has-patch dev-reviewed commit
Focuses: Cc:

Description

In a multilingual plugin, we need to filter the navigation menu outputted by the theme.

In Polylang, I am thus using the filter theme_mod_nav_menu_locations to populate the theme locations with the nav menus in the correct language.

Recent changes made to the customizer in WP 4.7 beta1 seem to introduce a backward compatibility issue as using this filter is now causing an infinite refresh loop in the customizer.

Here is a snippet to reproduce:

if ( ! is_admin() ) {
	add_filter( 'theme_mod_nav_menu_locations', 'pll_theme_mod_nav_menu_locations' );
}

function pll_theme_mod_nav_menu_locations( $menus ) {
	if ( is_array( $menus ) ) {
		foreach ( $menus as $loc => $menu ) {
			$menus[ $loc ] = 19; // some existing menu_id
		}
	}
	return $menus;
}

Attachments (1)

38612.0.diff (1.4 KB) - added by westonruter 10 months ago.

Download all attachments as: .zip

Change History (16)

#1 @westonruter
11 months ago

@Chouby This issue wasn't present in WordPress 4.6.1?

Relates to #37032.

#2 @Chouby
11 months ago

It indeed works in previous versions of WordPress.

I did not see #37032 before, but I see now that it was opened for WP 4.5 whereas I had no issue with this version.

#3 @westonruter
11 months ago

@Chouby ok, I think it may be something to do with the changes done for #27403, but I'm having a hard time pinpointing it. In any case, for an immediate fix while waiting on #37032, I suggest that you modify your code as follows:

<?php
function pll_remove_theme_mod_nav_menu_locations_filter() {
        remove_filter( 'theme_mod_nav_menu_locations', 'pll_theme_mod_nav_menu_locations' );
}
if ( ! is_admin() ) {
        add_filter( 'theme_mod_nav_menu_locations', 'pll_theme_mod_nav_menu_locations' );

        // Note that WP_Customize_Manager::customize_preview_settings() is called at wp_footer priority 10.
        add_action( 'wp_footer', 'pll_remove_theme_mod_nav_menu_locations_filter' );
}

The problem I think is that your filter is interfering with the customizer filters added for nav menus. So it is important to remove your filter before the customizer prints out the setting values so that it doesn't get stuck in that infinite loop.

Try that and let me know how you go.

#4 @westonruter
11 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #37032.

A fix for the underlying issue has been committed in [39112].

#5 @Chouby
11 months ago

Fantastic! I just tested the revision 39117 and it works as expected now.
Thanks!

#6 @SpencerFinnell
10 months ago

If anyone else is having this issue the same conflict exists with the latest versions of:

https://wordpress.org/plugins/nav-menu-roles/
https://wordpress.org/plugins/menu-items-visibility-control/

However

https://wordpress.org/plugins/if-menu/

Seems to work.

#7 @westonruter
10 months ago

  • Keywords needs-patch added
  • Milestone set to 4.7
  • Resolution duplicate deleted
  • Status changed from closed to reopened

@SpencerFinnell I can indeed reproduce issue in the Nav Menu Roles plugin:

  1. Given Twenty Seventeen activated with starter content imported
  2. Go to the nav menus admin page
  3. Set each of the social nav menu's items to only display if user is not logged in.
  4. Open customizer.
  5. Infinite refresh ensues.

#8 @westonruter
10 months ago

  • Owner set to westonruter
  • Status changed from reopened to accepted

#9 @westonruter
10 months ago

  • Keywords has-patch needs-testing added; needs-patch removed

@SpencerFinnell would you give 38612.0.diff a try?

#10 @SpencerFinnell
10 months ago

Yep, the patch fixes the issue for me.

#11 @westonruter
10 months ago

  • Keywords dev-feedback commit added; needs-testing removed

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


10 months ago

#13 @helen
10 months ago

  • Keywords dev-reviewed added; dev-feedback removed

#14 @westonruter
10 months ago

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

In 39510:

Customize: Prevent infinite full refresh from occurring when selective refresh falls back for a nav menu that has items excluded from rendering via filtering.

See #37032.
Fixes #38612.

#15 @westonruter
10 months ago

In 39511:

Customize: Prevent infinite full refresh from occurring when selective refresh falls back for a nav menu that has items excluded from rendering via filtering.

Merges [39510] onto 4.7 branch.
Fixes #38612 for 4.7.

Note: See TracTickets for help on using tickets.