Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editing menus in Customizer does not cause a refresh of the preview pane #105

Open
danielck opened this issue Feb 14, 2019 · 0 comments
Open

Comments

@danielck
Copy link
Contributor

When using a Dustpress-enabled theme and editing menus in the Customizer, the preview is not updated when menus are changed or new items are added. By default WordPress uses the postMessage transport method for updating menus, and presumably WP does not know where to look when the front end has been rendered using Dust templates.

A quick fix to this is to disable postMessage for menus and use the more robust refresh method instead, like this:

<?php
/**
 * Customizer customizations: some highly customized implementations of menus might not work correctly
 * with the default postMessage transport in the WordPress Customizer. This code will revert menus
 * to use the basic 'refresh' transport, which is slower but can be more reliable.
 *
 * @param \WP_Customize_Manager $wp_customize Core object for manipulating the Customizer.
 */
function my_customize_register( WP_Customize_Manager $wp_customize ) {
	// Use standard refresh for menus
	// Loop through menu locations
	$menu_locations = get_registered_nav_menus();
	foreach ( $menu_locations as $location => $description ) {
		$setting_id = "nav_menu_locations[{$location}]";
		$setting = $wp_customize->get_setting( $setting_id );
		if ( $setting ) {
			$setting->transport = 'refresh';
		}
	}
	// Loop through individual menus.
	$menus = wp_get_nav_menus();
	foreach ( $menus as $menu ) {
		$menu_id = $menu->term_id;
		$nav_menu_setting_id = 'nav_menu[' . $menu_id . ']';
		$nav_menu_setting             = $wp_customize->get_setting( $nav_menu_setting_id );
		if ( $nav_menu_setting ) {
			$nav_menu_setting->transport = 'refresh';
		}
		// Loop through menu items within menus.
		$menu_items = (array) wp_get_nav_menu_items( $menu_id );
		foreach ( array_values( $menu_items ) as $i => $item ) {
			// Create a setting for each menu item (which doesn't actually manage data, currently).
			$menu_item_setting_id = 'nav_menu_item[' . $item->ID . ']';
			$menu_item_setting              = $wp_customize->get_setting( $menu_item_setting_id );
			if ( $menu_item_setting ) {
				$menu_item_setting->transport = 'refresh';
			}
		}
	}
}
add_action( 'customize_register', 'my_customize_register' );

The above is just a proof of concept. I can create a PR for this if @Nomafin or @villesiltala can advise what would be the best place to put this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant