Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save westonruter/a9c6841ba7f28192e3eb7d90c9316e75 to your computer and use it in GitHub Desktop.
Save westonruter/a9c6841ba7f28192e3eb7d90c9316e75 to your computer and use it in GitHub Desktop.
WordPress Plugin for Customizer Cross-Domain Workaround (Trac #39128)
<?php
/**
* Plugin Name: Customizer Cross-Domain Workaround (Trac #39128)
* Description: Force customizer preview to use siteurl instead of home URL to work around Trac #39128. Temp fix for issue loading customizer preview when home and siteurl have different domains (cross-domain).
* Plugin URI: https://core.trac.wordpress.org/ticket/39128
* Author: Weston Ruter, XWP
* Author URI: https://make.xwp.co/
*
* Copyright (c) 2017 XWP (https://xwp.co/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
namespace Trac39128;
add_action( 'customize_register', function( \WP_Customize_Manager $wp_customize ) {
// Capture original home URL.
$home_url = get_option( 'home' );
// Override home URL to be the siteurl.
add_filter( 'option_home', function() {
return get_option( 'siteurl' );
} );
// Include the original home URL among the allowed URLs.
add_filter( 'customize_allowed_urls', function( $urls ) use ( $home_url ) {
$urls[] = $home_url;
return $urls;
} );
/*
* Ensure that the home URL will pass the wp_validate_redirect() check in \WP_Customize_Manager::set_preview_url().
* This is to ensure that when clicking Customize from the frontend the URL will be accepted.
*/
add_filter( 'allowed_redirect_hosts', function( $hosts ) use ( $home_url ) {
$hosts[] = parse_url( $home_url, PHP_URL_HOST );
return $hosts;
} );
/*
* Fix up the preview URL coming from the url query param when clicking Customize
* on the frontend to use the siteurl instead of the home URL.
*/
add_action( 'customize_controls_init', function() use ( $wp_customize ) {
$parsed_preview_url = parse_url( $wp_customize->get_preview_url() );
$preview_url = get_option( 'siteurl' );
if ( isset( $parsed_preview_url['path'] ) ) {
$preview_url .= $parsed_preview_url['path'];
} else {
$preview_url .= '/';
}
if ( isset( $parsed_preview_url['query'] ) ) {
$preview_url .= '?' . $parsed_preview_url['query'];
}
$wp_customize->set_preview_url( $preview_url );
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment