Multiple images upload Theme Customizer control – 02

After setting up some basic markup in an earlier post I will enable my control to actually do something with those buttons.

Seemingly useful control
Seemingly useful control

Hooking an action to the upload button

When the theme user clicks the Upload button WordPress media uploader should appear.
To make sure that the scripts and styles associated with media uploader are loaded before attempting any action I will use the enqueue method to load the media files

namespace tad\customizer\controls;

if (!class_exists('\WP_Customize_Control')) {
    return null;
class MultiImageControl extends \WP_Customize_Control
    public $type = 'multi-image';

    public function enqueue()


This method will override the parent class one, an empty method really, and will be executed any time the control is loaded.
Following along the line drawn in post by Mike Jolley I will create a javascript file to bind the media uploader to the button markup code echoed by MultiImageControl::theButtons method

    <a href="#" class="button-secondary multi-images-upload">
        <?php echo 'Upload'; ?>
    <a href="#" class="button-secondary multi-images-remove">
       <?php echo 'Remove images'; ?> 

the script is located in assets/js/backstretch_block_admin.js and actually does nothing but display the selected images urls

/*global console, jQuery, document, wp*/
jQuery(document).ready(function($) {
    "use strict";
    var file_frame;

        function(event) {

            // file frame already created, return
            if (file_frame) {

            // create the file frame
            file_frame = ={
                title: jQuery(this).data('uploader_title'),
                button: jQuery(this).data('uploader_button_text'),
                multiple: true,
                library: {
                    type: 'image'

            // get the selected attachments
            file_frame.on('select', function() {
                var selected = file_frame.state().get('selection').toJSON(),
                    urls = [];
                // store each image url in an hidden text field
                for (var i = selected.length - 1; i >= 0; i--) {

            // open the just created file frame

What’s happening now is that the Upload button will actually allow the selection of one or more images but will do nothing but log those urls to the console. That’s as unuseful as it can get and I will use the information in the next iteration.

2 thoughts on “Multiple images upload Theme Customizer control – 02

I appreciate your input