0

I'm implementing a gallery upload to my plugin, and I've set up an image gallery upload with:

function upload_gallery_button(e) {
    e.preventDefault();
    var $input_field = $('#portfolio_gallery');
    var ids = $input_field.val();
    var gallerysc = '[gallery ids="' + ids + '" link="file"]';
    wp.media.gallery.edit(gallerysc).on('update', function(g) {
        var id_array = [];
        $.each(g.models, function(id, img){
            id_array.push(img.id);
        });
        var ids = id_array.join(",");
        ids = ids.replace(/,\s*$/, "");
        $input_field.val(ids);
    });

When I click on upload button, the gallery upload opens, I select the images, and in my hidden input field I get the id's of the images that I can put in the gallery shortcode. But this only works when I save my page. Is it possible to pull the url's of the selected images so that the images are added instantly?

I don't need the gallery shortcode, I can create my own html markup for images in the metabox, but I'd like to have images shown as I add them.

I've tried changing id_array.push(img.id); to id_array.push(img.link); and id_array.push(img.url); but no luck.

The information on this is sparse, so any help is appreciated.

0

Found it:

id_array.push(img.attributes.url);

This will return url's instead of id's.

  • Another option is img.get('url') – Manny Fleurmond May 25 '15 at 13:47

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.