I have created a custom meta_box where I want to be able to select images for a gallery. The problem is, when I select images, I need to hold down Ctrl/Cmd to select more.

How can I do so this is possible?

I have tried changing the “multiple” to false, and removing it, but nothing happens.

        jQuery(document).ready(function() {
            var galleryFrame;
            var gallerySelection = [];
            var galleryImages = [];
            galleryImages = jQuery.parseJSON(jQuery('#mysite-gallery-array').val());

            // Frame
            galleryFrame = wp.media.frames.mysite_gallery_frame = wp.media({
                title: 'Select Image',
                button: {
                    text: 'Insert Image'
                },
                library: {
                    type: 'image'
                },
                multiple: true
            });

            // Add
            jQuery('#mysite-gallery-add').click(function(e) {
                e.preventDefault();
                galleryFrame.open();
            });

            // Open
            galleryFrame.on('open', function() {
                gallerySelection = galleryFrame.state().get('selection');

                if(galleryImages){
                    galleryImages.forEach(function(i) {
                        var attachment = wp.media.attachment(i);
                        gallerySelection.add(attachment ? [attachment] : []);
                    });
                }
            });

            // Select
            galleryFrame.on('select', function() {
                gallerySelection = galleryFrame.state().get('selection');

                jQuery("#mysite-gallery-main").empty();
                galleryImages = [];

                gallerySelection.map(function(attachment) {
                    var id = attachment.id;
                    var url = attachment.attributes.sizes.thumbnail.url;
                    galleryImages.push(id);

                    jQuery("#mysite-gallery-main").append('<div class="mysite-gallery-image"><input name="article_gallery[]" value="'+id+'" type="hidden"><img src="'+url+'"><div class="mysite-gallery-remove"></div></div>');
                });
            });

            // Remove
            jQuery('#mysite-gallery-main').on('click', '.mysite-gallery-remove', function() {
                var imageWrapper = jQuery(this).parents('.mysite-gallery-image')
                var imageId = imageWrapper.find('input').val();

                galleryImages.splice(jQuery.inArray(imageId, galleryImages), 1);
                imageWrapper.remove();
            });


            // Sortable
            jQuery('#mysite-gallery-main').sortable();
            jQuery('#mysite-gallery-main').disableSelection();
        });

Read more here: Select multiple images, WordPress


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: