1

I know I can listen for specific setting changes from the previewer by adding the following JS to the customize_preview_init hook:

wp.customize( 'setting-ID', function ( value ) {
    value.bind( function( val ) {    
      // things go here
    } );
} );

But can I easily listen for any changes? Or a group of changes? For example, I have a ton of settings that are all named something like: my_settings[setting_a], my_setting[setting_b]

1 Answer 1

1

Yes. You can bind to the change event on the entire Setting collection (wp.customize) as follows:

wp.customize.bind( 'change', function ( setting ) {
    if ( 0 === setting.id.indexOf( 'my_settings[' ) ) {
        doSomethingWithSettingValue( setting.get() );
    }
});
3
  • This is great, thanks! How do I grab the variable from the setting tho? I've tried: console.log(wp.customize.value(setting)); but that returns a function ƒ (){return f.instance.apply(f,arguments)}
    – rugbert
    Aug 25, 2017 at 22:54
  • 1
    Use setting.get(). I updated the answer. Aug 26, 2017 at 4:05
  • Thanks a lot @WestonRuter, for me you will always be the GRAND MASTER of the Customizer JS API. Correct me if I'm wrong, but 'my_settings[' should be 'my_setting_name' without [. It's what I've tried and worked for me. Thanks again !
    – LebCit
    May 23, 2020 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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