How to Create Sortable Checkboxes Option in Customizer
https://shellcreeper.com/how-to-create-sortable-checkboxes-option-in-customizer/
Last updated
https://shellcreeper.com/how-to-create-sortable-checkboxes-option-in-customizer/
Last updated
WordPress Customizer is a very powerful. Not only because we have a live preview, but also because it got a lot of build-in input type. And we can also extend it or create our own reuse-able input type.
This is not a full tutorial but only explaining the concept, but you can check the full working code here:
Download f(x) Share @GitHub
This is a useful control, we can use it for various settings for example:
Reorder element, such as header, navigation menu, thumbnail, excerpt, etc.
Sharing buttons.
etc..
So what we need to do is:
Figure out the data structure to save this settings,
Create custom customizer control.
Use it.
Note: you need to be familiar with Customizer API, and registering your own custom control. If not, you can read this:
In this sortable checkboxes data, we need to save all the checkboxes value (not only the selected checkbox, but all checkboxes), but we also need to save each as active (selected) or inactive (not selected)
for example we have sharing button: Facebook, Twitter, Google+, we can save the data in array:
To make it easier, I prefer to save the data as a string using comma “,” to separate the services (checkboxes) and use colon “:” for inactive/active status for each services. here’s the example data from the array above:
In this settings, i just create a hidden input, and the value is modified when user change the settings (clicking the checkboxes/sorting it).
To do this, we need to load control class in customize_register
hook:
This is what we need in the control class:
“enqueue” function
In this function we can enqueue scripts and styles needed for this control. and it will only loaded when this control is used.
We need to add “jquery” and “jquery-sortable” in our script dependency because we need that to enable user to sort/re-order the checkboxes.
We also need to add “customize-controls” script as dependency. I don’t really know why, but without it, the script will not working properly.
“render_content” function
In this function we can override how customizer render HTML for the settings. As you see above we have extra hidden input to save the real option.
The next part is to make the list of checkboxes sortable, and update the hidden input whenever a user change the value/re-order it.
Make the checkboxes sortable
because we already load “jquery-sortable” script, we can easily use that to make the checkboxes sortable by using the dashicon-menu as the handle:
We also trigger “change” when user reorder it, we are going to use it to update the value of the hidden input.
Updating the hidden input:
So we need to update the hidden input when “change” action is triggered in the input:
This will update the hidden input in the format as string format using comma “,” and colon “:” as separator. The “change” trigger to the hidden input is to update the live preview.
So what the JS do:
When sorting the checkboxes, trigger “change” to checkboxes input.
When “change” is triggered to the checkboxes, update the hidden input.
When modifying the hidden input, trigger change to update the live preview.
I create this because i need an easy to use sharing plugin, where my client can sort the order of the sharing button, and I think customizer is the best location to display this settings.
You can download and see the full code in my plugin:
Download f(x) Share @GitHub
I’m sure there’s a better way to build this feature, if you have a better solution please share in the comment below.
Source https://github.com/turtlepod/fx-share