So I have this plugin which associates images from the media upload modal to taxonomy terms via /wp-admin/edit-tags.php. The current version is in the branch named "button".

Basically, I've swapped out the "Insert in Post" button (input type="submit") with a custom "Add Thumbnail to Taxonomy" (span element) which calls an ajax script which creates an association between attachment and term. All of this works well.

The part that I cannot figure out is how to save information when a user makes changes to the attachment data (title, caption, etc.). While it's not really necessary for the plugin to do this ... more than a handful of users have requested this. And it makes sense to do because WordPress will do this when inserting into post content using the "Insert in Post" button.

I've been through the javascript and I don't believe that the request is being sent asynchronously. The only thing that comes to mind is that because the "Insert in Post" button is an actual submit button, WordPress is submitting the modal form in the background while it is closed for the user.

I've tried to implement similar functionality in my plugin by changing the span to a submit input, but this does no good at all. Firebug reports a 404 for my custom Ajax request and the modal does not close. This is really odd to me because the image still gets associated with the term.

Any help or suggestions would be greatly appreciated here!


WordPress has an hook for save meta data an attachment; an small example; like the post_data on save posts.

// Construct the attachment array
    $attachment = array_merge( array(
        'post_mime_type' => $type,
        'guid' => $url,
        'post_parent' => $post_id,
        'post_title' => $title,
        'post_content' => $content,
    ), $post_data );

    // Save the data
    $id = wp_insert_attachment($attachment, $file, $post_id);

See more on the code of media.php, function media_handle_upload For Update the data use the hook wp_update_attachment_metadata(), see source

Also you can add fields and data on attachment, maybe this post helps you.

| improve this answer | |
  • Thanks for the response, but I'm really looking to find out how the attachment data is saved when a user clicks the "Insert in Post" button from the modal window. If you test it out, you will see that any modified attachment data is updated in the database when user clicks this button and closes the modal. – mfields Apr 26 '11 at 18:29
  • Maybe this the function media_send_to_editor(); also it give an hook for change this: apply_filters('media_send_to_editor', $html, $send_id, $attachment); An example for add an function to this filter hook is the function image_media_send_to_editor(). maybe this helps you. – bueltge Apr 28 '11 at 21:48
  • Sorry for not excepting your answers, but I was looking for more of a javascript solution than a php one. For the next version of this plugin, I decided to mimic the functionality of the "featured image" controls instead of the "Insert into post". This enabled me to get the custom controls working across states in the media upload modal. The downside is that it does not save data like I originally wanted though. @goldenapples suggested just overriding the core javascript function which I plan on looking into next version. Thanks regardless! – mfields May 27 '11 at 5:41

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.