How to Use Conditional Logic and Live Preview in the Kirki Customizer Framework
Content
Working with the WordPress Customizer can be incredibly powerful, but managing complex conditional logic and achieving a smooth, live preview can be challenging. The Kirki Customizer Framework provides tools to make this easier, but understanding how to implement them is key. This guide covers the most common methods for showing/hiding controls and updating the preview without a full page reload.
The Core Concept: Conditional Controls
Many theme developers need to show or hide certain settings based on the value of another setting. For example, you might want to display a color picker only if a user has checked a "Customize Colors" toggle. Kirki simplifies this with the 'required' argument.
How to Implement the 'required' Argument
The 'required' argument allows you to define conditions that must be met for a control to be visible. It uses an array to specify the setting, a comparison operator, and the value to compare against.
$controls[] = array(
'type' => 'select',
'setting' => 'my_select_setting',
'label' => __( 'Select Demo', 'textdomain' ),
'section' => 'my_section',
'default' => 'option-1',
'choices' => array(
'option-1' => __( 'Option 1', 'kirki' ),
'option-2' => __( 'Option 2', 'kirki' ),
),
'required' => array(
array(
'setting' => 'my_toggle_setting', // The setting to check
'operator' => '==', // Comparison operator (==, !=, etc.)
'value' => true, // The value to compare against
),
),
);
In this example, the select control will only appear if the toggle control with the setting my_toggle_setting is checked (has a value of true).
Beyond the Basics: Custom Active Callbacks
While the 'required' argument is powerful, some conditions are too complex for its built-in comparisons. For instance, you might want to show a control only if a specific widget is active. The 'Kirki Customizer Framework' team has confirmed that Kirki does not have a built-in solution for this.
In such cases, you must use the native WordPress Customizer API and write a custom active callback function using 'active_callback'.
function my_custom_active_callback() {
// Perform your complex logic here
return ( is_active_widget( '', '', 'my-widget-id' ) ) ? true : false;
}
// In your control array
'active_callback' => 'my_custom_active_callback',
You may also need to write custom JavaScript to handle the live updating of these complex conditions in the Customizer preview.
Achieving a Live Preview (Without a Refresh)
A common frustration is the Customizer preview refreshing entirely when a simple change is made, causing you to lose your scroll position or state. The solution is to use the 'transport' method 'postMessage' and then handle the live update with JavaScript.
Step 1: Set the transport method for your control to 'postMessage'.
'transport' => 'postMessage',
Step 2: Enqueue a custom JavaScript file for the Customizer preview and write a script to listen for changes. For example, to change a body class based on a radio control:
wp.customize( 'theme_layout_control', function( value ) {
value.bind( function( new_layout ) {
// Remove any existing layout classes
var bodyClasses = jQuery( 'body' ).attr( 'class' );
bodyClasses = bodyClasses.replace( /layout-[a-zA-Z0-9_-]*/g, '' );
// Add the new class based on the selected value
jQuery( 'body' ).attr( 'class', bodyClasses ).addClass( 'layout-' + new_layout );
} );
} );
Important Limitations to Know
- Repeater Fields: Conditional logic (
'required') can be applied to hide an entire repeater control, but it cannot be used on individual sub-fields within a repeater. - Partial Refresh: For more complex live previews, like updating a background color, you can use the
'partial_refresh'argument. This is more advanced but offers greater control over what part of the preview is updated.
Conclusion
By combining Kirki's 'required' argument for simple conditions, WordPress's 'active_callback' for complex logic, and JavaScript with 'postMessage' for live previews, you can create a highly dynamic and user-friendly Customizer experience. Remember that some advanced scenarios may require custom JavaScript solutions beyond Kirki's built-in features.
Related Support Threads Support
-
Need help to select controllershttps://wordpress.org/support/topic/need-help-to-select-controllers/
-
‘button_labels’ support for image/upload fieldshttps://wordpress.org/support/topic/button_labels-support-for-imageipload-fields/
-
Refreshing view with hash in url (to stay at the anchor)https://wordpress.org/support/topic/refreshing-view-with-hash-in-url-to-stay-at-the-anchor/
-
active_callback on is_active_widgethttps://wordpress.org/support/topic/active_callback-on-is_active_widget/
-
Useful for plugin developers too?https://wordpress.org/support/topic/useful-for-plugin-developers-too/
-
pattern_replace Assistancehttps://wordpress.org/support/topic/pattern_replace-assistance/
-
A way to limit file types with upload control?https://wordpress.org/support/topic/a-way-to-limit-file-types-with-upload-control/
-
Update option by functionhttps://wordpress.org/support/topic/update-option-by-function/
-
Custom Color Pallet Controlhttps://wordpress.org/support/topic/custom-color-pallet-control/
-
Conditional controlshttps://wordpress.org/support/topic/conditional-controls/
-
tritogglehttps://wordpress.org/support/topic/tritoggle/
-
Any way to specify which page to load when Customizer section is clicked?https://wordpress.org/support/topic/any-way-to-specify-which-page-to-load-when-customizer-section-is-clicked/
-
Conditional control in repeater fieldhttps://wordpress.org/support/topic/conditional-control-in-repeater-field/
-
Question About Select Controlhttps://wordpress.org/support/topic/question-about-select-control/
-
toggle, checkbox and previewshttps://wordpress.org/support/topic/toggle-checkbox-and-previews/
-
Can i switch whole theme color with this framework?https://wordpress.org/support/topic/can-i-switch-whole-theme-color-with-this-framework/
-
Partial Refresh for background-colorhttps://wordpress.org/support/topic/partial-refresh-for-background-color/
-
How to pull whether sortable option is visible or not?https://wordpress.org/support/topic/how-to-pull-whether-sortable-option-is-visible-or-not/
-
Tyography control in repeater and translationhttps://wordpress.org/support/topic/tyography-control-in-repeater-and-translation/
-
Preview in custmizer when radio-image changehttps://wordpress.org/support/topic/preview-in-custmizer-when-radio-image-change/
-
SVG Code in Radio Image Choiceshttps://wordpress.org/support/topic/svg-code-in-radio-image-choices/
-
Code Control / Shortcodeshttps://wordpress.org/support/topic/code-control-shortcodes/
-
Output field value only if another field has some valuehttps://wordpress.org/support/topic/output-field-value-only-if-another-field-has-some-value/