1

I need to use WP media uploader to upload multiple images inside my plugin. I found this code which works fine for a single image. I've tried changing multiple: false to true, which enabled me to select multiple images in the media uploader. My problem is I don't know how to get urls of all uploaded images, not just one as it is in the attached code. I've tried changing var uploaded_image = image.state().get('selection').first(); to var uploaded_image = image.state().get('selection') and then iterating over the array with this code, but had no success.

var uploaded_image = image.state().get('selection').first();
            jQuery.each(uploaded_image, function (i) {
                console.log(uploaded_image[i].toJSON().url);
            });

Original code:

jQuery(document).ready(function($){
$('#upload-btn').click(function(e) {
    e.preventDefault();
    var image = wp.media({
        title: 'Nahranie obsahu',
        // mutiple: true if you want to upload multiple files at once
        multiple: false
    }).open()
        .on('select', function(e){
            // This will return the selected image from the Media Uploader, the result is an object
            var uploaded_image = image.state().get('selection').first();

            // We convert uploaded_image to a JSON object to make accessing it easier
            // Output to the console uploaded_image
           // console.log(uploaded_image);
            var image_url = uploaded_image.toJSON().url;
            // Let's assign the url value to the input field
            $('#image_url').val(image_url);
        });
});
});
1

Got it working with a code from this question https://stackoverflow.com/questions/14847668/get-url-of-freshly-uploaded-image-from-wp3-5-media-uploader

Maybe somebody will find it useful. This code logs URL of every freshly uplaoded image to console.

jQuery(document).ready(function($){
$('#upload-btn').click(function(e) {
    e.preventDefault();
    var image = wp.media({
        title: 'Nahranie obsahu',
        // mutiple: true if you want to upload multiple files at once
        multiple: true
    }).open()
        .on('select', function(e){
            // This will return the selected image from the Media Uploader, the result is an object
            var uploaded_images = image.state().get('selection');

            var attachment_ids = uploaded_image.map( function( attachment ) {
                attachment = attachment.toJSON();
                console.log(attachment.url);
            }).join();

            // We convert uploaded_image to a JSON object to make accessing it easier
            // Output to the console uploaded_image
           // console.log(uploaded_image);
            var image_url = uploaded_image.toJSON().url;
            // Let's assign the url value to the input field
            $('#image_url').val(image_url);
        });
});
});
  • Key typos in this (uploaded_image instead of uploaded_images), but still a somewhat useful framework, so thanks! – CK MacLeod Jan 4 '17 at 22:27

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.