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.

enter image description here

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 wp_handle_upload_prefilter or 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!

1 Answer 1


I found it after trying with several hooks.

Use 'init' and check if $_FILES is empty, if not, do what you want to do.

init hook

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.