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?

1 Answer 1

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 Commented Sep 9, 2016 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.