plugin development – How to run a function as soon as the image is uploaded?

Question

I have a function that reprocesses an image after it is uploaded to a website. I want this function to be run when any image is uploaded to the website through adding media in the post or uploading the image directly from the image gallery.

What hook do I need to use to do this? I have tried with add_attachment, wp_handle_upload and wp_handle_upload_prefilter but it doesn’t work. Whenever an image is uploaded through adding to the post, it gives an error that the image could not be uploaded.

Below is an example of my code. Every time I select an image to add to a post, I get a message like this. Please help me.
Thank you very much!

An error occurred in the upload. Please try again later.

function custom_to_all_image($upload) {
    if (wp_attachment_is_image($attachment_id)) {
        echo $upload['type'];
    }
    return $upload;
}
add_filter( 'wp_handle_upload', 'custom', 10, 2 );

0
Tom J Nowell 1 month 2023-05-02T11:29:15-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse