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

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() );
    }
});
  • 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 '17 at 22:54
  • 1
    Use setting.get(). I updated the answer. – Weston Ruter Aug 26 '17 at 4:05

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.