Search code examples
phpwordpressimageuser-roles

How to make a check in the wordpress library: did the user or the function upload the image to the admin panel?


I have the website on wordpress and function replace_uploaded_image in functions.php which uploads images from external url and transforms their original sizes to thumbnail sizes.

But I wouldn't like to made this transformations when I directly upload images in admin library.

What condition do I need to supplement the function or on which hook to hang?

function replace_uploaded_image($image_data) {

  if (!isset($image_data['sizes']['thumbnail'])) return $image_data;

  // paths to the uploaded image and the thumbnail image
  $upload_dir = wp_upload_dir();
  $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];

  $current_subdir = substr($image_data['file'],0,strrpos($image_data['file'],"/"));
  $thumbnail_image_location = $upload_dir['basedir'] . '/'.$current_subdir.'/'.$image_data['sizes']['thumbnail']['file'];

  // delete the uploaded image
  unlink($uploaded_image_location);

  // rename the thumbnail image
  rename($thumbnail_image_location,$uploaded_image_location);

  // update image metadata and return them
  $image_data['width'] = $image_data['sizes']['thumbnail']['width'];
  $image_data['height'] = $image_data['sizes']['thumbnail']['height'];
  unset($image_data['sizes']['thumbnail']);

  return $image_data;
}

add_filter('wp_generate_attachment_metadata','replace_uploaded_image');

Solution

  • To resolve this issue first I was need to add second parametr $attachment_id to function replace_uploaded_image and then find out if the attachment has post_author.

    Full code:

    function replace_uploaded_image($image_data, $attachment_id) {
    
      // Check if the image was uploaded via the WordPress admin panel
      $attachment = get_post( $attachment_id );
    
      if (isset($attachment->post_author)) {
        return $image_data; // Return the unmodified image data
      }
    
      // if there is no thumbnail image : return
      if (!isset($image_data['sizes']['thumbnail'])) return $image_data;
    
      // paths to the uploaded image and the thumbnail image
      $upload_dir = wp_upload_dir();
      $uploaded_image_location = $upload_dir['basedir'] . '/' .$image_data['file'];
      // $thumbnail_image_location = $upload_dir['path'] . '/'.$image_data['sizes']['thumbnail']['file']; // ** This only works for new image uploads - fixed for older images below.
      $current_subdir = substr($image_data['file'],0,strrpos($image_data['file'],"/"));
      $thumbnail_image_location = $upload_dir['basedir'] . '/'.$current_subdir.'/'.$image_data['sizes']['thumbnail']['file'];
    
      // delete the uploaded image
      unlink($uploaded_image_location);
    
      // rename the thumbnail image
      rename($thumbnail_image_location,$uploaded_image_location);
    
      // update image metadata and return them
      $image_data['width'] = $image_data['sizes']['thumbnail']['width'];
      $image_data['height'] = $image_data['sizes']['thumbnail']['height'];
      unset($image_data['sizes']['thumbnail']);
    
      return $image_data;
    }
    
    add_filter('wp_generate_attachment_metadata','replace_uploaded_image', 10, 2 );