admin管理员组

文章数量:1122832

I have a function to add EXIF information to an image after the image is uploaded to a website. How do I get the EXIF information to be automatically added to the image after the image is uploaded or added to the post?

I have tried some hooks like the following to check for failed photo uploads and started executing the function to add EXIF info but it doesn't work. add_attachment, wp_handle_upload and wp_handle_upload_prefilter. 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)) {
      //my function
    }
    return $upload;
}
add_filter( 'wp_handle_upload', 'custom', 10, 2 );

I have a function to add EXIF information to an image after the image is uploaded to a website. How do I get the EXIF information to be automatically added to the image after the image is uploaded or added to the post?

I have tried some hooks like the following to check for failed photo uploads and started executing the function to add EXIF info but it doesn't work. add_attachment, wp_handle_upload and wp_handle_upload_prefilter. 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)) {
      //my function
    }
    return $upload;
}
add_filter( 'wp_handle_upload', 'custom', 10, 2 );
Share Improve this question edited May 3, 2023 at 13:18 Kelvin Ngo asked May 2, 2023 at 16:29 Kelvin NgoKelvin Ngo 113 bronze badges 7
  • A filter only changes existing data - the problem is likely that you're trying to echo. Try writing to a log file instead - something that doesn't directly output data to the browser. – WebElaine Commented May 2, 2023 at 16:53
  • it doesn't make sense to echo on image upload as those happen during AJAX calls and there'd be no way to see your output, it'd also break the AJAX response! That's why the post editor says image could not be uploaded. What are you trying to do that requires this? It sounds like an X Y problem were you asked how to implement your fix not how to solve the problem. I read the initial few sentences that try to explain this but it didn't make much sense. What does reprocessing do? Are you trying to optimise images? Making your Q more specific will get you more/better answers – Tom J Nowell Commented May 2, 2023 at 16:55
  • @TomJNowell I'm just giving an example so people can imagine my purpose, not me calling echo. :( What I need is this, I want the image after uploading to add some EXIF attributes and to do this I need to recreate the image.The image reconstruction I tested when running the separate function it worked successfully, just now I don't know how to check when the image is successfully uploaded to the website to start executing the function to add the EXIF attribute and recreate the image. – Kelvin Ngo Commented May 2, 2023 at 17:49
  • ah then you should ask how to add extra EXIF data when images are uploaded, that's much easier to understand and answer, as it's very specific. You should edit your question to refocus it on adding EXIF data on upload – Tom J Nowell Commented May 2, 2023 at 18:26
  • @TomJNowell Thank you! I have edited my question again – Kelvin Ngo Commented May 3, 2023 at 1:09
 |  Show 2 more comments

1 Answer 1

Reset to default 1

Update: Edited code to make it work

function proccess_to_all_image($file) {
    $file_path = $file['file'];
    $mime_type = $file['type'];
    $id = attachment_url_to_postid($file['url']);
    if (preg_match('/^image\/[a-z\-]+$/i', $mime_type)) {
      *//call to function here*
    }
    return $file;
}
add_filter( 'wp_handle_upload', 'proccess_to_all_image', 10, 2);

本文标签: plugin developmentHow to add extra EXIF data when images are uploaded