Using WordPress ‘customize_render_partials_response’ PHP filter

The customize_render_partials_response WordPress PHP filter allows plugins to inject $scripts and $styles as dependencies for the partials being rendered. The response data will then be available to the client via the render-partials-response JS event.

Usage

add_filter('customize_render_partials_response', 'your_function_name', 10, 4);
function your_function_name($response, $refresh, $partials) {
    // your custom code here
    return $response;
}

Parameters

  • $response (array): The response containing the contents and errors.
  • $refresh (WP_Customize_Selective_Refresh): The selective refresh component.
  • $partials (array): Placements’ context data for the rendered partials.

More information

See WordPress Developer Resources: customize_render_partials_response

Examples

Injecting custom styles

Inject custom styles into the DOM for the partials being rendered.

add_filter('customize_render_partials_response', 'inject_custom_styles', 10, 4);
function inject_custom_styles($response, $refresh, $partials) {
    $response['styles'] = '<style>.my-custom-style { color: red; }</style>';
    return $response;
}

Injecting custom scripts

Inject custom scripts into the DOM for the partials being rendered.

add_filter('customize_render_partials_response', 'inject_custom_scripts', 10, 4);
function inject_custom_scripts($response, $refresh, $partials) {
    $response['scripts'] = '<script>console.log("Hello, world!");</script>';
    return $response;
}

Modifying response contents

Modify the contents of the response before it’s sent to the client.

add_filter('customize_render_partials_response', 'modify_response_contents', 10, 4);
function modify_response_contents($response, $refresh, $partials) {
    $response['contents']['my_partial_id'] = 'Modified content';
    return $response;
}

Handling response errors

Handle errors that occurred during the rendering of partials.

add_filter('customize_render_partials_response', 'handle_response_errors', 10, 4);
function handle_response_errors($response, $refresh, $partials) {
    if (!empty($response['errors'])) {
        // handle errors here
    }
    return $response;
}

Adding custom data to the response

Add custom data to the response to be used on the client-side.

add_filter('customize_render_partials_response', 'add_custom_data', 10, 4);
function add_custom_data($response, $refresh, $partials) {
    $response['custom_data'] = 'Some custom data';
    return $response;
}