---
description: >-
  https://shellcreeper.com/how-to-create-sortable-checkboxes-option-in-customizer/
---

# How to Create Sortable Checkboxes Option in Customizer

## How to Create Sortable Checkboxes Option in Customizer

[David](https://shellcreeper.com/author/scdave/)[1](https://shellcreeper.com/how-to-create-sortable-checkboxes-option-in-customizer/#comments) Oct 26, 2015

![Customizer-Sortable-Checkboxes](https://shellcreeper.com/blog/wp-content/uploads/2015/10/Customizer-Sortable-Checkboxes.png)

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](https://github.com/turtlepod/fx-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:

1. Figure out the data structure to save this settings,
2. Create custom customizer control.
3. Use it.

Note: you need to be familiar with Customizer API, and registering your own custom control. If not, you can read this:

* [WP Codex](https://codex.wordpress.org/Theme_Customization_API)
* [WP Developer](https://developer.wordpress.org/themes/advanced-topics/customizer-api/)
* [Creating Custom Customizer Control (Otto)](http://ottopress.com/2012/making-a-custom-control-for-the-theme-customizer/)

### Data structure:

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:

```
$data = array(   'facebook' => true,   'twitter'  => true,   'google'   => false,);
```

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:

```
facebook:1,twitter:1,google:0
```

### How to save the data:

In this settings, i just create a hidden input, and the value is modified when user change the settings (clicking the checkboxes/sorting it).

### Let’s register a custom customizer control:

To do this, we need to load control class in `customize_register` hook:

```
add_action( 'customize_register', 'my_customizer_register' ); /** * Register */function my_customizer_register( $wp_customize ){     /**     * Custom Customizer Control: Sortable Checkboxes     * @link https://shellcreeper.com/?p=1503     */    class My_Customize_Control_Sortable_Checkboxes extends WP_Customize_Control{         /* Add functions here. */     }     /* Add Section (Optional) */    $wp_customize->add_section();     /* Add Setting */    $wp_customize->add_setting();     /* Add Control (use the class here) */    $wp_customize->add_control();}
```

### Custom Control Class:

This is what we need in the control class:

```
/*** Custom Customizer Control: Sortable Checkboxes* @link https://shellcreeper.com/?p=1503*/class My_Customize_Control_Sortable_Checkboxes extends WP_Customize_Control{     /**     * Scripts for this control     */    public function enqueue(){        wp_enqueue_style( 'css-for-this-control' );        wp_enqueue_script( 'js-for-this-control' );    }     /**     * HTML     */    public function render_content(){        /* Bail if no option */        if ( empty( $this->choices ) ){ return; }         /* HTML */        ?>        <ul class="my-multicheck-sortable-list">            <li>                <label>                    <input name="facebook" type="checkbox" value="1" checked="checked"> Facebook                </label>                <i class="dashicons dashicons-menu"></i>            </li>            <li>                <label>                    <input name="twitter" type="checkbox" value="1" checked="checked"> Twitter                </label>                <i class="dashicons dashicons-menu"></i>            </li>            <li>                <label>                    <input name="google_plus" type="checkbox" value="0"> Google+                </label>                <i class="dashicons dashicons-menu"></i>            </li>             <?php /* HIDDEN INPUT TO SAVE THE ACTUAL DATA */ ?>            <input type="hidden" value="facebook:1,twitter:1,google:0">         </ul>    <?php    }}
```

**“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.

### Javacript to Sort and Update Hidden Input

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:

```
$( 'ul.my-multicheck-sortable-list' ).sortable({    handle: 'ul.my-multicheck-sortable-list .dashicons-menu',    axis: 'y',    update: function( e, ui ){        $('ul.my-multicheck-sortable-list input').trigger( 'change' );    }});
```

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:

```
$( "ul.my-multicheck-sortable-list li input" ).on( 'change', function(){     /* Get the value, and convert to string. */    this_checkboxes_values = $( this ).parents( 'ul.my-multicheck-sortable-list' ).find( 'li input' ).map( function(){        var active = '0';        if( $(this).prop("checked") ){            var active = '1';        }        return this.name + ':' + active;    }).get().join( ',' );   /* Add the value to hidden input. */   $( this ).parents( 'ul.my-multicheck-sortable-list' ).find( 'input[type="hidden"]' ).val( this_checkboxes_values ).trigger( 'change' ); });
```

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:**

1. When sorting the checkboxes, trigger “change” to checkboxes input.
2. When “change” is triggered to the checkboxes, update the hidden input.
3. 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](https://github.com/turtlepod/fx-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](https://github.com/turtlepod/fx-share)

{% file src="https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LWPtKFbhw0XehxVuoiZ%2Fuploads%2FlIcDOz9krvMAgYrBFI4Z%2Ffx-share-master.zip?alt=media&token=cd716d6d-adb6-4408-9e1e-aafef54279d2" %}
