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);
});
});
});