Hook for validating and rejecting frontend image upload
I am making a plugin that checks the contents of uploaded images. Right now I am testing if I am using the right hook, so I use
getimagesize() to check the image (I am aware this is not the best/most thorough way to do this. This is just for testing the hook). If it’s not an image, it will show the error message below.
I use the hook
wp_handle_upload_prefilter and it works fine, but only at the admin dashboard (which is what the codex suggests). I used this plugin which allows users to upload from post/page to do the testing and the fake image uploaded successfully.
I’ve searched through Google and this site and most people suggest
add_attachment. However, I am not sure
add_attachment is the one I am looking for because according to this site an attachment is a file uploaded from the post edit screen, which is still from admin dashboard. I’ve tested it and the error message did show up. (ps. I see people use
add_filter("add_attachment",...) but WordPress says this is an action hook?)
I’m also thinking just go through each file in $_FILES and then do the checking, but again I don’t know which hook to use with this method.
Thanks very much!