I’m building a WordPress theme with a custom settings page. Some of the settings require the user to upload/add a set of images (more than 1). The default behavior of the media uploader is to upload and/or select a single image and insert it into a post.
I’ve followed this excellent guide on utilizing the media uploader, and it suggests that I should be able to set multiple to true, but it still only allows for a single file to be uploaded or selected.
Here’s my code:
Load in the needed scripts since this is a custom settings page:
if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}else{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
Javascript/jQuery For displaying the media uploader and handling the selected images:
var tgm_media_frame;
$('.upload-images').click(function() {
if ( tgm_media_frame ) {
tgm_media_frame.open();
return;
}
tgm_media_frame = wp.media.frames.tgm_media_frame = wp.media({
multiple: true,
library: {
type: 'image'
},
});
tgm_media_frame.on('select', function(){
var selection = tgm_media_frame.state().get('selection');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
console.log(attachment);
// Do something with attachment.id and/or attachment.url here
});
});
tgm_media_frame.open();
});
Has anyone been able to get multiple file selection working properly? Am I missing something? Thanks!
You just need to change
multiple:true
tomultiple:'add'
. This is how default Create Gallery works.Update
I think media uploader has been updated since question asked and answered. I guess in previous version of wordpress
multiple: 'add'
option was not present as suggested by other users. I’m not sure enough.Everything’s good with your code. Just a small part missing.
After
attachment
convertedtoJSON
you can use it as mentioned in documentation. You can place image urls into some hidden fields posting to server and display selected images to user same time.Just a small thing I feel weird is that, we need to press ctrl+click to select images. It should be rather checkboxes or something.
update
ctrl+click or shift+click for selecting multiple images is the way wordpress have given. For a closer look, open
There’s a function defined –
toggleSelectionHandler
. It listens for key combination user has pressed and accordingly calls other function which changes certain classes and cause actual selection.After inspecting into firbug, you can see two classes are applied —
selected
details
details
class defines styles for current clicked/active selection ( with blue thick border) andselected
actually marks image as selected and returns it in selection array.You can alter that behaviour either from that file itself or write your own function to handle selection. First approach is not good though.
PS : WordPress does not actually use above file. It chooses compressed version of same file. So you might want to load uncompressed file and play around. You can force wordpress to use uncompressed js files by setting
in
wp-config.php
. This will skip behaviour ofload-scripts.php
(loading compressedversion of every js file into single file by merging them ).
In case anyone wants to know how to do this, one way is this. Note that it will completely override the default behavior within the scope it is implemented.
If
multiple
is set totrue
then this will allow you to select multiple items, like in the gallery screen.