2

I'm trying to use media manager in metabox, and I need to use multiple select. I just have problem with doing output from that selection. I need element in metabox to hold all selected files urls. My code is here:

jQuery(document).ready(function($){
var custom_uploader;
$('#upload_image_button').click(function(e) {
    e.preventDefault();
    //If the uploader object has already been created, reopen the dialog
    if (custom_uploader) {
        custom_uploader.open();
        return;
    }
    //Extend the wp.media object
    custom_uploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose Image',
        button: {
            text: 'Choose Image'
        },
        multiple: true
    });
    custom_uploader.on('select', function() {
        selection.map( function( attachment ) {
        attachment = attachment.toJSON();
        $("#obal").after("<img src=" +attachment.url+">");
        });
    });
    custom_uploader.open();
});
});

What's wrong with it?

7

It was just my mistake... I've forgotten improve var selection

jQuery(document).ready(function($){
  var custom_uploader;
  $('#upload_image_button').click(function(e) {
    e.preventDefault();
    //If the uploader object has already been created, reopen the dialog
    if (custom_uploader) {
      custom_uploader.open();
      return;
    }
    //Extend the wp.media object
    custom_uploader = wp.media.frames.file_frame = wp.media({
      title: 'Choose Image',
      button: {
        text: 'Choose Image'
      },
      multiple: true
    });
    custom_uploader.on('select', function() {
      var selection = custom_uploader.state().get('selection');
      selection.map( function( attachment ) {
        attachment = attachment.toJSON();
        $("#obal").after("<img src=" +attachment.url+">");
      });
    });
    custom_uploader.open();
  });
});
1
  • works like charm, edited little bit to fit with my requirement, thanks Sep 9 '16 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.