Selective Refresh

Classes for implementing selective (partial) refresh in the Customizer preview.


WP_Customize_Selective_Refresh

Core class for implementing selective refresh. Manages partial registration and handles Ajax rendering requests.

Source: wp-includes/customize/class-wp-customize-selective-refresh.php
Since: 4.5.0
Final: Yes
Attributes: #[AllowDynamicProperties]

Constants

ConstantValueDescription
RENDER_QUERY_VAR'wp_customize_render_partials'POST query var used in partial render requests

Properties

PropertyTypeVisibilityDefaultSinceDescription
$managerWP_Customize_Managerpublic4.5.0Customize manager instance
$partialsWP_Customize_Partial[]protectedarray()4.5.0Registered partial instances
$triggered_errorsarrayprotectedarray()4.5.0Log of errors during partial rendering
$current_partial_idstring|nullprotected4.5.0Currently-rendering partial ID

Methods

MethodVisibilitySinceDescription
__construct( $manager )public4.5.0Stores manager, requires class-wp-customize-partial.php, hooks init_preview to customize_preview_init.
partials()public4.5.0Returns all registered partials.
add_partial( $id, $args )public4.5.0Adds a partial. Accepts WP_Customize_Partial object or ID + args. Applies customize_dynamic_partial_args and customize_dynamic_partial_class filters when constructing from args.
get_partial( $id )public4.5.0Returns partial by ID, or null.
remove_partial( $id )public4.5.0Removes a partial by ID.
init_preview()public4.5.0Hooks handle_render_partials_request to template_redirect and enqueue_preview_scripts to wp_enqueue_scripts.
enqueue_preview_scripts()public4.5.0Enqueues customize-selective-refresh script and hooks export_preview_data to wp_footer at priority 1000.
export_preview_data()public4.5.0Exports partials JSON, renderQueryVar, and localized strings to JS via inline script _customizePartialRefreshExports. Switches to user locale for l10n.
add_dynamic_partials( $partial_ids )public4.5.0Registers partials dynamically. For each ID, applies customize_dynamic_partial_args filter (must return non-false array) and customize_dynamic_partial_class filter. Returns array of newly-added partials.
is_render_partials_request()public4.5.0Returns true if $_POST[RENDER_QUERY_VAR] is set.
handle_error( $errno, $errstr, $errfile, $errline )public4.5.0Error handler during rendering. Logs error with partial ID, number, string, file, and line. Always returns true.
handle_render_partials_request()public4.5.0Main Ajax handler. Validates customize preview, decodes $_POST['partials'] JSON, registers dynamic partials, renders each partial for its container contexts, and sends JSON response with contents, errors (if WP_DEBUG_DISPLAY), and setting_validities.

Hooks

HookTypeSinceDescription
customize_dynamic_partial_argsfilter4.5.0Filters dynamic partial constructor args. Must return non-false array for partial to be registered.
customize_dynamic_partial_classfilter4.5.0Filters the class used to construct partials. Default 'WP_Customize_Partial'.
customize_render_partials_beforeaction4.5.0Fires immediately before partials are rendered. Plugins can enqueue scripts/styles here.
customize_render_partials_afteraction4.5.0Fires immediately after partials are rendered. Plugins can scrape footer scripts here.
customize_render_partials_responsefilter4.5.0Filters the response array. Plugins can inject $scripts and $styles dependencies.

Localized Strings (exported to JS)

KeyText
shiftClickToEdit"Shift-click to edit this element."
clickEditMenu"Click to edit this menu."
clickEditWidget"Click to edit this widget."
clickEditTitle"Click to edit the site title."
clickEditMisc"Click to edit this element."
badDocumentWrite"document.write() is forbidden"

WP_Customize_Partial

Representation of a rendered region in the previewed page that gets selectively refreshed when an associated setting changes.

Source: wp-includes/customize/class-wp-customize-partial.php
Since: 4.5.0
Attributes: #[AllowDynamicProperties]

Properties

PropertyTypeVisibilityDefaultSinceDescription
$componentWP_Customize_Selective_Refreshpublic4.5.0Parent selective refresh component
$idstringpublic4.5.0Unique identifier. Typically matches the associated setting ID.
$id_dataarrayprotected4.5.0Parsed ID with base and keys for multidimensional settings
$typestringpublic'default'4.5.0Partial type
$selectorstringpublic4.5.0jQuery selector to find the container element
$settingsstring[]public4.5.0IDs for settings tied to this partial. Defaults to [$id].
$primary_settingstringpublic4.5.0ID of the primary setting. Defaults to first setting.
$capabilitystringpublic4.5.0Required capability. Empty = derived from settings.
$render_callbackcallablepublic4.5.0Render callback. Called with ($partial, $container_context). Can echo or return string/array, or return false on error. Defaults to [$this, 'render_callback'].
$container_inclusiveboolpublicfalse4.5.0Whether the container element is included in the partial
$fallback_refreshboolpublictrue4.5.0Whether to do a full refresh if partial render fails

Methods

MethodVisibilitySinceDescription
__construct( $component, $id, $args )public4.5.0Accepts args matching property names. Parses ID into id_data. Defaults render_callback to $this->render_callback(). Normalizes $settings to array; defaults to [$id]. Sets $primary_setting to first setting if empty.
id_data()public final4.5.0Returns parsed $id_data array with base and keys.
render( $container_context )public final4.5.0Calls $render_callback, captures output buffer. Warns if callback both echoes and returns. Applies customize_partial_render and customize_partial_render_{$partial->id} filters. Returns string, array, or false.
render_callback( $partial, $context )public4.5.0Default callback — returns false (no render). Subclasses override this.
json()public4.5.0Returns array with settings, primarySetting, selector, type, fallbackRefresh, containerInclusive.
check_capabilities()public final4.5.0Returns false if user lacks $capability or if any associated setting fails check_capabilities().

Hooks

HookTypeSinceDescription
customize_partial_renderfilter4.5.0Filters partial rendering result. Params: $rendered, $partial, $container_context.
customize_partial_render_{$partial->id}filter4.5.0Filters partial rendering for a specific partial ID. Same params.