How to save attachment data?

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.

Read More

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!

Related posts

Leave a Reply

1 comment

  1. 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.