Getting ID from ajax response of async-upload.php

Its a wordpress+jQuery question. Can’t post it on stackoverflow because i believe it involves wordpress knowledge too.

What I am trying to achieve:
I am attaching the wordpress async media uploader on “add post” page metabox so user can upload lots of images simply drag and drop them into the uploader and don’t have to insert them once at a time (see the image).

Read More

image uploader

I am saving the attachment ids on a post meta so user can attach one image to more then one post.

Question: When the uploader send the upload request to the async-upload.php it sends back an html response which contains the whole form for adding title, caption, description for the attachment. Very first of the response comes with this:

<input type='hidden' id='type-of-106' value='image' />

You can see a full response here.

My question is how to I get the attachment id from that html response? I am guessing I have to listen for any ajax response that comes from the async-upload.php then look through the response.

Related posts

Leave a Reply

2 comments

  1. Can you specify the type of response sent back? JSON would definitely be preferred here over HTML. If not, I don’t know if you’ve got a “reliable” way to grab the ID, but you can use jQuery to find it. Here is an example that you would put in your success function for the Ajax request:

    success: function(html){
        var item_info = $(html).find('.media-item-info').attr('id');
        var info_array = item_info.split('-');
        var attach_id = info_array[2];
    }
    

    Again, I wouldn’t say that this is the most reliable method because you are assuming that ‘.media-item-info’ will be present every time, but since I don’t have a full view of your code, this is what I will go by.