WP Customizer set a value via javascript (to create presets)

The solution is to simply enqueue the JS value syncing snippet so that it is added in the customizer pane (customize.php) instead of the customizer preview window (on the frontend). In other words, enqueue your JS at customize_controls_enqueue_scripts instead of wp_enqueue_scripts. Here’s one snippet:

wp.customize( 'field1', 'field2', function( field1, field2 ) {
    field1.bind( function( value ) {
        field2.set( value );
    } );
} );

And here’s an even more succinct example that does the same thing:

wp.customize( 'field1', 'field2', function( field1, field2 ) {
    field2.link( field1 );
} );

Nevertheless, if you have the same value stored in multiple settings, why not just re-use the same setting in multiple places?

Leave a Comment