-
-
Save florianbrinkmann/e44e62eb77333e8e080d7ac605b91da0 to your computer and use it in GitHub Desktop.
;(function () { | |
/** | |
* Run function when customizer is ready. | |
*/ | |
wp.customize.bind('ready', function () { | |
wp.customize.control('slug_select_control', function (control) { | |
/** | |
* Run function on setting change of control. | |
*/ | |
control.setting.bind(function (value) { | |
switch (value) { | |
/** | |
* The select was switched to the hide option. | |
*/ | |
case 'hide': | |
/** | |
* Deactivate the conditional control. | |
*/ | |
wp.customize.control('slug_conditional_control').deactivate(); | |
break; | |
/** | |
* The select was switched to »show«. | |
*/ | |
case 'show': | |
/** | |
* Activate the conditional control. | |
*/ | |
wp.customize.control('slug_conditional_control').activate(); | |
break; | |
} | |
}); | |
}); | |
}); | |
})(); |
Works well with checkboxes, but $wp_customize->add_setting
must have 'transport' => 'postMessage'
setting instead of the default 'transport' => 'refresh'
Detailed here: https://codex.wordpress.org/Theme_Customization_API#Part_3:_Configure_Live_Preview_.28Optional.29
I would like to keep the refresh option. Would there be any possibility?
@CA-Irag and others who have faced the same problem is because of the lack of server side active_callback
for the control. This jQuery only makes active/inactive on real time front end HTML. You also need to setup a active_callback
for the desired control.
Here is more details on it - https://make.xwp.co/2016/07/24/dependently-contextual-customizer-controls/
Works once, then, when Customizer get refresh, the control appear again. Don't know how to solve this.
Found the solution, instead of use the method deactivate and activate, use toggle like this:
wp.customize.control('my_control', function (control) {
const toggleControl = (value) => {
if (condition) {
wp.customize.control('control_to_hide').toggle(true);
} else {
wp.customize.control('control_to_hide').toggle(false);
}
};
toggleControl(control.setting.get());
control.setting.bind(toggleControl);
});
Found the solution, instead of use the method deactivate and activate, use toggle like this:
wp.customize.control('my_control', function (control) { const toggleControl = (value) => { if (condition) { wp.customize.control('control_to_hide').toggle(true); } else { wp.customize.control('control_to_hide').toggle(false); } }; toggleControl(control.setting.get()); control.setting.bind(toggleControl); });
condition undefined?
@lwxbr Cheers that worked!
Same problem as CA-Irag