0

I'm using Plupload and making use of wp-admin/async-upload.php to allow my website's users to upload images from the front-end asynchronously. The default behaviour of that particular script is for files to be uploaded to the media library as posts (of post_type attachment). Users may upload as many files as they like (each file upload creates a new post).

My aim is to allow users to continue to upload in this way but instead of creating a new post on each upload, I'd like the previously uploaded file to be replaced. Hence, users will have at most 1 post each in the system.

I don't see anything in wp-admin/async-upload.php that will allow me to hook in and add this custom behaviour.

My question

Is there a way to modify the default behaviour of asynchronous uploads? Or will I need to write my own async-upload.php script?

  • 1
    Looking through the async-uploads file it calls a function wp_ajax_upload_attachment(), this function checks to see if the post ID has been passed ( $_REQUEST['post_id'] ). It looks like if you were to add the post_id of the previous attachment you want to edit/replace, you may be able to modify that attachment rather than creating a new one. – Matt Keys Nov 8 '14 at 14:37
  • Thanks Matt.. you could be on to something there! I'll check it out... – henrywright Nov 8 '14 at 14:38
  • I'm 98.76% sure Plupload will let me pass the post ID via multipart_params so this looks like it could be do-able. I'll report back.. – henrywright Nov 8 '14 at 14:41
  • Ah, I don't think this is possible. Drilling down, the post ID is passed to media_handle_upload() which uses it as the post_parent – henrywright Nov 8 '14 at 14:46
  • Bummer, I think you will have to go custom here then. You could fish around for some filters in the attachment creation process so maybe you don't have to rewrite everything. It looks like things eventually end up at wp_insert_post() in post.php, which has a couple filters you could investigate like wp_insert_attachment_data – Matt Keys Nov 8 '14 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.