Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

How to add Selective Refresh in the Customizer?

Open therakib7 opened this issue 9 years ago • 3 comments

Hello, In WordPress new version 4.7 added a new feature selective refresh for the customizer. like the screenshot http://prntscr.com/dm28tg

Is any option to add it dynamically on unyson? Is not, I request you to add this features in next update. Thanks

therakib7 avatar Dec 21 '16 07:12 therakib7

Hello,

We haven't inspected this feature yet.

ghost avatar Dec 21 '16 10:12 ghost

I found this article by a WP dev: https://make.wordpress.org/core/2016/03/22/implementing-selective-refresh-support-for-widgets/

This plugin implements it https://wordpress.org/plugins/customize-partial-refresh/

It works around the concept of partials, using get_template_part() to render the "partial", which is simply a partial block in the frontend, with a set of options in the backend. Pretty much the same thing we have going with shortcodes, where view.php and options.php would be a partial.

The partial displays a link related to the controls in the customizer. Supposedly, the page can then refresh selectively, only the partials involved.

I haven't tried or looked anything into this, but that article seems a good start.

albertso avatar Jan 02 '17 21:01 albertso

How to enable Selective Refresh for Unyson Customizer Options

  1. Enable Twenty Seventeen theme

  2. Create {theme}/framework-customizations/theme/options/customizer.php

    $options = array(
    	'test' => array(
    		'type' => 'text',
    		'value' => 'Hello World!',
    		'wp-customizer-setting-args' => array(
    			'transport' => 'postMessage'
    		),
    	)
    );
    
  3. Edit {theme}/header.php and at the end add:

    <div class="unyson-test">
    	<?php echo fw_get_db_customizer_option('test'); ?>
    </div>
    
  4. Add in {theme}/functions.php

    function _action_theme_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( 'fw_options[test]', array(
    		'selector' => '.unyson-test',
    		'render_callback' => function() { // Anonymous functions don't work in PHP 5.2!
    			return fw_get_db_customizer_option('test');
    		},
    	) );
    }
    add_action( 'customize_register', '_action_theme_partials' );
    

ghost avatar Jan 09 '17 12:01 ghost