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).
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.
I could but i really don’t want to post the work of others here, even if it’s open source code.
Rilwis Meta Box Class has a plupload field, which has a custom upload handler.
If you mind to take a look: https://github.com/rilwis/meta-box
resp: https://github.com/rilwis/meta-box/blob/master/inc/fields/plupload-image.php#L31
The answer is there. This code,or at least the idea, gives you much more control over the response.
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:
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.