Skip to content

Instantly share code, notes, and snippets.

@danielck
Last active February 14, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielck/50539b4dfd87f56d0d4bdd45621c0419 to your computer and use it in GitHub Desktop.
Save danielck/50539b4dfd87f56d0d4bdd45621c0419 to your computer and use it in GitHub Desktop.
Disable postMessage for built-in WordPress menus in Customizer
<?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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment