Front end form with images and media_handle_upload logic?

Getting your form to upload an image as soon as it is selected rather that submitted is pretty straightforward, as you can submit the form whenever a field changes like this:

<form action="http://example.com" method="post" action="#">
  <input type="file" onchange="form.submit()" />
</form>

The difficulty is in keeping track of this at the server end. The user may change his mind and select another image. So you only want to keep the things that are in the form when the user clicks on the submit button. If there is only one image this is still fairly easy: delete all attachments you created except for the last one. But if there are multiple file input fields your administration becomes slightly more complicated.

However, if you look at media_handle_upload you see that there is a possibility to pass $post_data to the file you are uploading. You could, for instance, use the excerpt field (temporarily) to store you administration. To get this admin data from the form to the server you could use hidden fields. Your form could look something like this:

<form action="http://example.com" method="post" action="#">
  // hidden field with the id of the post that is generated
  <input type="hidden" name="post_id" id="post_id" value="99999" />
  <input type="file" name="image_upload1" id="image_upload1" onchange="form.submit()" />
  <input type="file" name="image_upload2" id="image_upload2" onchange="form.submit()" />
  <input id="submit_my_image" name="submit_my_image" type="submit" value="Upload" />
</form>

Now, when the form is submitted (either by javascript or a submit action) there is data arriving at the server telling you the name of a file to be uploaded. You create the attachment to the post:

$post_data['excerpt'] = 'image1';
$file_id = media_handle_upload("image_upload1", $application_id, $post_data);
$post_data['excerpt'] = 'image2';
$file_id = media_handle_upload("image_upload2", $application_id, $post_data);

If the user changes his mind often you will have quite a lot of attachments, with the same $post_data but different time stamps. You can do the clean up by looping through all attachments at the real submit. Or you can do a test whether a file with the same excerpt already exist before you call to media_handle_upload above.

Now you must distinguish between the javascript submits and the final submit. My suggestion would be to change the name of the submit button on hover/click. At this point you could also clean up de excerpts.

Now, what if the user abandons the process altogether? The above will only work if you create a post the moment you serve the form (or at least at the first image upload, as you cannot have an attachment to a non existing post). The trick here is to define your own post_status, which you set during your call to wp_insert_post. Only when the user submits you change the post_status to draft or publish. Periodically you do an automated clean up of all posts, including attachments, of that self-defined type that are more than three hours old or so.

techhipbettruvabetnorabahisbahis forumutaraftarium24eduseduseduseduedueduedusedueduedu