How do I prevent a widget being updated in the customizer preview if a widget field fails a JS validation check? More generally, is there any actual documentation for the customizer JS API? (I mean real, proper api documentation rather than the few examples or sketchy descriptions)
-
1I'm having the same problem. My widget has a checkbox field, every time this field is checked and unchecked the whole customizer preview reloads. Instead I want to use the js hooks like partial-content-rendered.– s_ferdieMay 30, 2016 at 9:36
1 Answer
Visit this link and read about transporter
argument: https://codex.wordpress.org/Theme_Customization_API
Is that what you are looking for?
Here is an example for you:
<?php
add_action( 'customize_register', 'my_customizer' );
function my_customizer($wp_customize){
$wp_customize->add_section( 'my_section',
array(
'title' => __( 'My Custom Section', 'mytheme' ), //Visible title of section
'capability' => 'edit_theme_options', //Capability needed to tweak
)
);
$wp_customize->add_setting( 'my_setting',
array(
'default' => 'option1', //Default setting/value to save
'type' => 'theme_mod', //Is this an 'option' or a 'theme_mod'?
'capability' => 'edit_theme_options', //Optional. Special permissions for accessing this setting.
'transport' => 'postMessage', //What triggers a refresh of the setting? 'refresh' or 'postMessage' (instant)?
)
);
$wp_customize->add_control(
'my_control',
array(
'label' => __( 'My Custom Control', 'mytheme' ),
'section' => 'my_section',
'settings' => 'my_setting',
'type' => 'radio',
'choices' => array(
'option1' => 'Option 1',
'option2' => 'Option 2',
),
)
);
}
-
Could you provide an example of how to implement this please? The link is a great reference.– Howdy_McGee ♦Oct 28, 2016 at 19:13
-
-
Thanks :D ! The rule of thumb for answers is that all the relevant information for the answer should be in the content itself and not in external links - those are additional references. That sounds like what he's referring to - there's an
edit
link underneath your answer to add in all the relevant information. There's also some documentation on code formatting ( usually just highlight and click the code button ). +1– Howdy_McGee ♦Oct 28, 2016 at 19:34