WordPress Customizer: Using Multiple Customize Settings in Single Control

In a recent WordPress project i worked on, i needed to have an input field and a select dropdown close to each other. Below is a screenshot of what i was looking to achieve.

WordPress input and select customize controls

I had two ideas of how i would pull this off but none of them quite frankly pleased me. Here are the ideas and why i used neither.

* Create customize setting and control for the input and select fields since core already has support for input and select fields.

Going this route would have meant not being able add inline style to set the width property of the select dropdown to auto so it doesn’t have an 100% value added by Customizer to virtually every of its core controls.
Adding inline style is possible for input fields using the input_attrs argument.

* Create custom controls for the input and select fields to allow for the flexibility i craved for.

Going this route would have meant writing lots of code (register the customize settings and coding the PHP classes for the custom control for each fields) to achieve something this simple.

Reading the WordPress Customizer documentation, one would think that the standard is: for every customize settings, there can only be one corresponding customize control.

Controls are the primary Customizer object for creating UI. Specifically, every control must be associated with a setting, and that setting will save user-entered data from the control to the database (in addition to displaying it in the live-preview and sanitizing it).

I dug into the customizer components codes and discovered one or more customize settings can be used or integrated to a single customize control.

To achieve my goal, i ended up doing this – register customize settings for both fields and then combined them in a single a custom control. Let’s see some code.

The code below adds or register customize settings for both the input and select dropwdown fields.

And below is the custom control PHP class. The trick of being able to use multiple settings in a single control is in $this->value('schedule_digit') $this->link('schedule_digit'); and $this->value('schedule_type') $this->link('schedule_type') which Render the data link attribute and fetches the settings’ value for each control.

Note that schedule_digit and schedule_type are array keys holding the value of the input and select customize settings. For a better understanding of their usefulness, see the code below which add the custom control created by WP_Customize_Schedule_Fields_Control PHP class to customizer interface.

If you have any question or contribution, use the comment form below.

La Fin!

Don't miss my updates.
Subscribe to Receive Programming, Web development, Git and WordPress Tutorials & all things tech.