1

Is it possible to get a default value that was set in the Customizer setting? Here is an example of the code:

// Add setting.
$wp_customize->add_setting( 'slug_awesome_title', array(
    'default' => esc_html__( 'WordPress', 'slug' ),
    'sanitize_callback' => 'sanitize_text_field',
) );

// Add control.
$wp_customize->add_control( 'slug_awesome_title', array(
    'label' => esc_html__( 'Section Title', 'slug' ),
    'section' => 'slug_awesome_section',
    'type' => 'text',
) );

If the current control has already a set value, let's say it's "WooCommerce", how can I get a default value which is set to "WordPress" using wp.customize API in JS?

I've tried this:

wp.customize.instance( 'slug_awesome_title' ).get()

but it returns the current value of the control.

0

It would make sense for the default value to be available via:

wp.customize.instance( 'slug_awesome_title' ).default

Or equivalently:

wp.customize( 'slug_awesome_title' ).default

However, I can see the default is simply not getting exported from PHP to JS. I can see that this is going to be needed in core also when client-side templating replaces server-side rendering of controls in #30738, so a core patch would be needed.

Nevertheless, in the mean time you can add the following PHP to export your default value onto the JS Setting object to have mostly the same effect:

add_action( 'customize_controls_enqueue_scripts', function() {
    global $wp_customize;
    wp_add_inline_script( 'customize-controls', sprintf(
        'wp.customize( "slug_awesome_title", function ( setting ) { setting.default = %s; } );',
        wp_json_encode( $wp_customize->get_setting( 'slug_awesome_title' )->default )
    ) );
} );
0

You can access all default settings by calling the object:

wp.customize.settings.values

For example:

// Enqueue live preview javascript in Theme Customizer admin screen
wp.customize( 'my_control', function( value ) {

    console.log( "default: ", wp.customize.settings.values.my_control );

    value.bind( function( newval ) {
        // ....
    } );
} );
  • wp.customize.settings.values.my_control will return current value, not default value. – gdfgdfg Dec 9 '18 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.