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 can select multiple media files at once by holding down the control key (on PC) or the command key (on Mac) while clicking multiple files in the upload window. You can also drag and drop media files from your computer directly into the Media Library.
You may need to add the function wrapper in your plugin/theme file. This is the following: // UPLOAD ENGINE function load_wp_media_files() { wp_enqueue_media(); } add_action( 'admin_enqueue_scripts', 'load_wp_media_files' ); This will call the relevant JS files and CSS files if WP fails to load the upload manager.
You just need to change multiple:true
to multiple:'add'
. This is how default Create Gallery works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With