WordPress Customizer postMessage Transport: Instant Live Preview Without Page Reload

Instant Customizer previews with the postMessage transport

The Customizer shows the site in an iframe next to the settings. By default every change reloads that iframe: the page is requested again with the new value, which takes a second or more on a real site. For settings that only change a colour, a text or a class, the postMessage transport is much faster: the new value is sent to the preview with JavaScript, and a small script changes the page in place.

Problem: Every change of the theme’s primary colour in the Customizer reloads the preview, and choosing a colour becomes a slow trial and error.

Solution: Register the setting with 'transport' => 'postMessage', load a script in the preview on customize_preview_init that binds to the setting with wp.customize(), and apply the value there, ideally to a CSS custom property that the theme’s CSS uses.

The setting

<?php
add_action( 'customize_register', function ( WP_Customize_Manager $wp_customize ) {
    $wp_customize->add_section( 'ha_colors', array(
        'title'    => __( 'Brand colours', 'textdomain' ),
        'priority' => 30,
    ) );

    $wp_customize->add_setting( 'ha_primary_color', array(
        'default'           => '#0066cc',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'postMessage', // no reload
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'ha_primary_color', array(
        'label'   => __( 'Primary colour', 'textdomain' ),
        'section' => 'ha_colors',
    ) ) );
} );

The CSS on the server

The saved value is printed as a custom property, so the whole theme uses one variable:

<?php
add_action( 'wp_enqueue_scripts', function () {
    $color = sanitize_hex_color( get_theme_mod( 'ha_primary_color', '#0066cc' ) );
    wp_add_inline_style( 'ha-style', ':root { --ha-primary: ' . $color . '; }' );
}, 20 );
.button,
.site-header {
    background-color: var(--ha-primary);
}

The preview script

<?php
add_action( 'customize_preview_init', function () {
    wp_enqueue_script(
        'ha-customizer-preview',
        get_template_directory_uri() . '/js/customizer-preview.js',
        array( 'customize-preview' ),
        filemtime( get_template_directory() . '/js/customizer-preview.js' ),
        true
    );
} );

js/customizer-preview.js runs inside the preview iframe:

( function ( api ) {
    api( 'ha_primary_color', function ( setting ) {
        setting.bind( function ( value ) {
            document.documentElement.style.setProperty( '--ha-primary', value );
        } );
    } );

    // A text setting: replace the text of an element
    api( 'blogdescription', function ( setting ) {
        setting.bind( function ( value ) {
            var el = document.querySelector( '.site-description' );
            if ( el ) {
                el.textContent = value;
            }
        } );
    } );
} )( wp.customize );

Because the CSS uses the custom property, the script changes one value and every element follows. Without custom properties the script would have to repeat the selectors of the stylesheet, and the preview and the saved result would drift apart over time.

Selective refresh

For settings whose output is rendered in PHP (a menu, a widget area, a footer text with shortcodes), JavaScript cannot rebuild it. Selective refresh, with $wp_customize->selective_refresh->add_partial(), reloads just that part of the page over AJAX and keeps the rest; it is the middle ground between a full refresh and postMessage.

NOTE: postMessage changes only the preview. What visitors see after publishing comes from PHP, so the server-side output and the preview script must produce the same result. Using one CSS custom property for both, as above, is the easiest way to keep them in sync.