Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maheshwaghmare/f4f32bbeb2c79d4767fb30f54c83a8a5 to your computer and use it in GitHub Desktop.
Save maheshwaghmare/f4f32bbeb2c79d4767fb30f54c83a8a5 to your computer and use it in GitHub Desktop.
/**
* Helper showControlIfhasValues()
*/
function showControlIfhasValues( setting, ExpectedValues ) {
return function( control ) {
// Check the current value in the array of ExpectedValues
var isDisplayed = function() {
return $.inArray( setting.get(), ExpectedValues ) !== -1;
};
var setActiveState = function() {
control.active.set( isDisplayed() );
};
control.active.validate = isDisplayed;
setActiveState();
setting.bind( setActiveState );
};
}
/**
* Test Control Dependency
*/
wp.customize( 'select-control-1', function( setting ) {
// Show control 'text-control-1' if control 'select-control-1' value has 'one' or 'two'
wp.customize.control( 'text-control-1', showControlIfhasValues( setting, ['one','two']) );
// Show control 'text-control-2' if control 'select-control-1' value has 'one'.
wp.customize.control( 'text-control-2', showControlIfhasValues( setting, ['one']) );
// Show control 'text-control-2' if control 'select-control-1' value has 'one'.
wp.customize.control( 'text-control-4', showControlIfhasValues( setting, ['one']) );
// Show control 'text-control-2' if control 'select-control-1' value has 'one' or 'three'.
wp.customize.control( 'text-control-3', showControlIfhasValues( setting, ['one', 'three']) );
} );
@kmob2
Copy link

kmob2 commented Dec 6, 2016

This is great. I appreciate it. Thank you!

@uNkset
Copy link

uNkset commented Oct 10, 2018

god bless you!

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