Using WordPress ‘customize_render_partials_before’ PHP action

The customize_render_partials_before WordPress PHP action fires before partials are rendered, allowing plugins to perform tasks such as enqueueing scripts and styles.

Usage

add_action('customize_render_partials_before', 'my_custom_code', 10, 2);

function my_custom_code($refresh, $partials) {
  // your custom code here
}

Parameters

  • $refresh (WP_Customize_Selective_Refresh): The selective refresh component.
  • $partials (array): An array of placements’ context data for the partials rendered in the request. The array is keyed by partial ID, with each item being an array of the placements’ context data.

More information

See WordPress Developer Resources: customize_render_partials_before

Examples

Enqueue a custom script

Enqueue a custom script before rendering partials:

add_action('customize_render_partials_before', 'enqueue_my_custom_script', 10, 2);

function enqueue_my_custom_script($refresh, $partials) {
  wp_enqueue_script('my-custom-script', get_template_directory_uri() . '/js/my-custom-script.js', array('jquery'), '1.0.0', true);
}

Enqueue a custom style

Enqueue a custom style before rendering partials:

add_action('customize_render_partials_before', 'enqueue_my_custom_style', 10, 2);

function enqueue_my_custom_style($refresh, $partials) {
  wp_enqueue_style('my-custom-style', get_template_directory_uri() . '/css/my-custom-style.css', array(), '1.0.0');
}

Add a data attribute to a specific partial

Add a custom data attribute to a specific partial before rendering:

add_action('customize_render_partials_before', 'add_data_attribute_to_partial', 10, 2);

function add_data_attribute_to_partial($refresh, $partials) {
  if (isset($partials['my-partial-id'])) {
    $partials['my-partial-id']['data-my-attribute'] = 'custom-value';
  }
}

Modify a partial’s context data

Modify the context data of a specific partial before rendering:

add_action('customize_render_partials_before', 'modify_partial_context_data', 10, 2);

function modify_partial_context_data($refresh, $partials) {
  if (isset($partials['my-partial-id'])) {
    $partials['my-partial-id']['my_context_key'] = 'new-value';
  }
}

Remove a specific partial

Remove a specific partial before rendering:

add_action('customize_render_partials_before', 'remove_specific_partial', 10, 2);

function remove_specific_partial($refresh, $partials) {
  if (isset($partials['my-partial-id'])) {
    unset($partials['my-partial-id']);
  }
}