partial_refresh

WordPress 4.5 introduced partial (or selective) refreshes to the customizer.

As documented in announcement on make.wordpress.org/core, the syntax that we have to use would look like this:


function my_register_blogname_partials( WP_Customize_Manager $wp_customize ) {

    // Abort if selective refresh is not available.
    if ( ! isset( $wp_customize->selective_refresh ) ) {
        return;
    }

    $wp_customize->selective_refresh->add_partial( 'header_site_title', [
        'selector'        => '.site-title a',
        'settings'        => [ 'blogname' ],
        'render_callback' => function() {
            return get_bloginfo( 'name', 'display' );
        },
	] );

    $wp_customize->selective_refresh->add_partial( 'document_title', [
        'selector'        => 'head > title',
        'settings'        => [ 'blogname' ],
        'render_callback' => 'wp_get_document_title',
	] );
}
add_action( 'customize_register', 'my_register_blogname_partials' );

Kirki simplifies that process and you can add the arguments for partial refreshes in your fields using the partial_refresh argument. The example from above when combined with a text field would become like this:

Kirki::add_field( 'my_config', [
	'type'            => 'text',
	'settings'        => 'my_setting',
	'label'           => esc_html__( 'Text Control', 'kirki' ),
	'section'         => 'my_section',
	'default'         => esc_html__( 'This is a default value', 'kirki' ),
	'priority'        => 10,
	'partial_refresh'    => [
		'header_site_title' => [
			'selector'        => '.site-title a',
			'render_callback' => function() {
				return get_bloginfo( 'name', 'display' );
			},
		],
		'document_title' => [
			'selector'        => 'head > title',
			'render_callback' => 'wp_get_document_title',
		],
	],
] );

Can't find what you're looking for? Check the github issues or edit this page to add what's missing.