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!