{"id":3783,"date":"2017-06-24T13:27:31","date_gmt":"2017-06-24T11:27:31","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=3783"},"modified":"2020-02-09T10:59:48","modified_gmt":"2020-02-09T09:59:48","slug":"conditional-displaying-and-hiding-of-customizer-controls-via-javascript","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/conditional-displaying-and-hiding-of-customizer-controls-via-javascript-3783\/","title":{"rendered":"Conditional displaying and hiding of customizer controls via JavaScript"},"content":{"rendered":"\n

When using Partial Refresh<\/em> in the customizer to reload only a part of the preview after changing a setting\u2019s value, the active_callback for these settings is not working anymore. With active_callback<\/code> you can, for example, show a control only if another control has a particular value. In that case, the conditional displaying has to be done with JavaScript.<\/p>\n\n\n\n\n\n\n\n

The customizer comes with an API, which lets us activate and deactivate (show and hide) controls in a relatively straightforward way. In this tutorial, we are going to display or hide a control, depending on the value of a select list control. At first, the complete code (also available as a Gist<\/a>. You can enqueue it via the customize_controls_enqueue_scripts<\/code> action):<\/p>\n\n\n

;(function<\/span> ()<\/span> <\/span>{\n\t\/**\n\t * Run function when customizer is ready.\n\t *\/<\/span>\n\twp.customize.bind('ready'<\/span>, function<\/span> ()<\/span> <\/span>{\n\t\twp.customize.control('slug_select_control'<\/span>, function<\/span> (control)<\/span> <\/span>{\n\t\t\t\/**\n\t\t\t * Run function on setting change of control.\n\t\t\t *\/<\/span>\n\t\t\tcontrol.setting.bind(function<\/span> (value)<\/span> <\/span>{\n\t\t\t\tswitch<\/span> (value) {\n\t\t\t\t\t\/**\n\t\t\t\t\t * The select was switched to the hide option.\n\t\t\t\t\t *\/<\/span>\n\t\t\t\t\tcase<\/span> 'hide'<\/span>:\n\t\t\t\t\t\t\/**\n\t\t\t\t\t\t * Deactivate the conditional control.\n\t\t\t\t\t\t *\/<\/span>\n\t\t\t\t\t\twp.customize.control('slug_conditional_control'<\/span>).deactivate();\n\t\t\t\t\t\tbreak<\/span>;\n\t\t\t\t\t\/**\n\t\t\t\t\t * The select was switched to \u00bbshow\u00ab.\n\t\t\t\t\t *\/<\/span>\n\t\t\t\t\tcase<\/span> 'show'<\/span>:\n\t\t\t\t\t\t\/**\n\t\t\t\t\t\t * Activate the conditional control.\n\t\t\t\t\t\t *\/<\/span>\n\t\t\t\t\t\twp.customize.control('slug_conditional_control'<\/span>).activate();\n\t\t\t\t\t\tbreak<\/span>;\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\t});\n})();<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

We listen to the ready<\/code> event of the customizer and hook a function to the control with the ID slug_select_control<\/code>, which was created with the options hide<\/code> and show<\/code> via $wp_customize->add_control()<\/code>. With control.setting.bind<\/code>, we can now call a function after the value of the control has changed.<\/p>\n\n\n\n

After a change of the setting, we run a switch<\/code> statement and show or hide the slug_conditional_control<\/code> control with the activate()<\/code> respectively deactivate()<\/code> method.<\/p>\n","protected":false},"excerpt":{"rendered":"

When using Partial Refresh in the customizer to reload only a part of the preview after changing a setting\u2019s value, the active_callback for these settings is not working anymore. With active_callback you can, for example, show a control only if another control has a particular value. In that case, the conditional displaying has to be […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","lazy_load_responsive_images_disabled":false},"categories":[115],"tags":[],"wp-worthy-pixel":{"ignored":false,"public":"477b689c408b438c8d3c226234461d12","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/477b689c408b438c8d3c226234461d12"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3783"}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=3783"}],"version-history":[{"count":2,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3783\/revisions"}],"predecessor-version":[{"id":5877,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3783\/revisions\/5877"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=3783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=3783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=3783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}