Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress 3.5 Media Uploader Multiple File Select

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!

like image 456
Cory Avatar asked Jul 04 '13 15:07

Cory


People also ask

How do you select multiple files from the media library in Wordpress?

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.

How do I add custom media uploader button to wordpress admin?

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.


1 Answers

You just need to change multiple:true to multiple:'add'. This is how default Create Gallery works.

like image 64
Dmitry Semenov Avatar answered Sep 28 '22 00:09

Dmitry Semenov