Search code examples
wordpressmediaautosave

Attach Media to Post without Autosaving First


With WordPress, I'm using wp_editor to allow users to use the editor and media uploader. I'd like to avoid creating an auto-save of the post, but the problem is that attachments have no post_id to attach to until the post is saved.

I was wondering if there was a way to upload the media and then attach it to the new post after it has been saved.

Could I pass a temporary ID to the attachment to identify it when the post is saved? Or maybe grab the attachment ID after it's been uploaded?

Ideas appreciated!


Solution

  • After much fussing, I came to the conclusion that the post ID upfront is necessary.

    I ended up doing what the WordPress Post Edit screen does. I used a little jQuery and Ajax to dynamically create an autosave once the user has typed in a post title.

    Once the autosave is done, it replaces the media upload iframe href with one containing the autosave post ID.

    So, there's still a chance that there will be some abandoned post drafts, but at least it requires a little commitment by typing in the post title.