Skip to content

Instantly share code, notes, and snippets.

@soderlind
Last active May 13, 2025 01:29
Show Gist options
  • Save soderlind/edf9f162e148867f98d0ff23fd6ad390 to your computer and use it in GitHub Desktop.
Save soderlind/edf9f162e148867f98d0ff23fd6ad390 to your computer and use it in GitHub Desktop.
Enable WordPress Customizer Preview on Mapped Domain
<?php
/**
* Plugin Name: Enable WordPress Customizer Preview on Mapped Domain.
*
*/
/**
* Prevents redirection to mapped domain when the WordPress Customizer is active.
*
* When WordPress Domain Mapping is active, it typically redirects all requests
* to the mapped domain. This code modifies that behavior to allow access to
* the Customizer on non-mapped domains by:
*
* 1. Checking if the WP_Customize_Manager class exists
* 2. Checking if domain mapping functionality is active (via redirect_to_mapped_domain function)
* 3. Removing the standard domain mapping redirect action
* 4. Adding a modified redirect action that doesn't redirect when Customizer preview is being loaded
*
* This allows administrators to use the Customizer on the original domain while
* maintaining normal domain mapping functionality for all other requests.
*/
add_action( 'plugins_loaded', function () {
// The WP_Customize_Manager class is loaded when the Customizer is active
// This is important to ensure that the Customizer is available before we
// attempt to modify its behavior
if ( ! class_exists( 'WP_Customize_Manager' ) ) {
return;
}
// Check if the domain mapping functionality is active
if ( function_exists( 'redirect_to_mapped_domain' ) ) {
// Remove the standard domain mapping redirect
remove_action( 'template_redirect', 'redirect_to_mapped_domain' );
// Add a custom redirect that excludes Customizer preview requests
add_action( 'template_redirect', function () {
// Check if this is a Customizer preview request by examining query parameters
// The customize_theme parameter indicates Customizer is active
// The customize_messenger_channel with 'preview-0' value is specific to preview mode
if ( isset( $_GET[ 'customize_theme' ] ) &&
isset( $_GET[ 'customize_messenger_channel' ] ) &&
$_GET[ 'customize_messenger_channel' ] == 'preview-0' ) {
return; // Skip redirection for Customizer preview
}
// For all other requests, perform the normal domain mapping redirect
redirect_to_mapped_domain();
} );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment