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;
}
});
});
});
})();
@mrdulal
Copy link

mrdulal commented Mar 19, 2018

Didn't work with toggle type. Tired a lot but still no luck.

@akramulhasan
Copy link

Same problem as CA-Irag

@thomastognacci
Copy link

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

@basterrika
Copy link

I would like to keep the refresh option. Would there be any possibility?

@amitbiswas06
Copy link

@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/

@lwxbr
Copy link

lwxbr commented Mar 18, 2019

Works once, then, when Customizer get refresh, the control appear again. Don't know how to solve this.

@lwxbr
Copy link

lwxbr commented Mar 18, 2019

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);
});

@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