How to extend Customizer payload sent when ‘Save & Publish’ is triggered

By default, customized sent the below payload when the Save & Publish button is clicked.

wp_customize:on
theme:twentysixteen
customized:{"header_color[primary]":"345"}
nonce:d2a36386d3
action:customize_save

Is there a way i can extend the payload to include custom data to be posted?

Refereence: https://github.com/WordPress/WordPress/blob/master/wp-admin/js/customize-controls.js#L3425

1 Answer
1

You can monkeypatch the wp.customize.previewer.query method:

add_action( 'customize_controls_enqueue_scripts', function () {
    wp_add_inline_script( 'customize-controls', '(function ( api ) {
        api.bind( "ready", function () {
            var _query = api.previewer.query;

            api.previewer.query = function () {
                var query = _query.call( this );
                query.foo = "bar";
                return query;
            };
        });
    })( wp.customize );'
    );
});

This will ensure the script runs immediately after customize-controls.js in the markup – note that wp_add_inline_script will wrap your JS within <script /> tags, no need to do it yourself.

Leave a Comment