Skip to content

Instantly share code, notes, and snippets.

@florianbrinkmann
Last active December 28, 2022 23:29
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save florianbrinkmann/e44e62eb77333e8e080d7ac605b91da0 to your computer and use it in GitHub Desktop.
Save florianbrinkmann/e44e62eb77333e8e080d7ac605b91da0 to your computer and use it in GitHub Desktop.
Show or hide WordPress customizer control on change of another control. https://florianbrinkmann.com/en/3783/conditional-displaying-and-hiding-of-customizer-controls-via-javascript/
;(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;
}
});
});
});
})();
@pgroot91
Copy link

pgroot91 commented Aug 27, 2020

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?

@iamlasse
Copy link

iamlasse commented Mar 2, 2021

@lwxbr Cheers that worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment