1

Is there a way to prompt an editor whenever they try to upload an image smaller than 350px width? Essentially rejecting any image uploads that are smaller than 350px width?

Thanks

2

Here you go:

   if(!current_user_can('delete_others_posts')){
    /*Handling wp media uloads*/
    add_filter('wp_handle_upload_prefilter','lets_handle_img_width');
    function lets_handle_img_width($file)
    {
        $img = getimagesize($file['tmp_name']);
        $width = $img[0];

        if ($width < 350){
            $file['error'] = "Image is small too small. Get something of width more than 350px.";
        }
        return $file;
    }
}

Check official documentation about wp_handle_upload_prefilter.

  • Do you really need the else and return $file twice? – BillK Feb 7 '16 at 0:16
  • Yes It is. Try to remove, you will get long error message :) btw I'm not returning false twice. It's a condition. – Rituparna sonowal Feb 7 '16 at 0:34
  • Well, looking at the code logically it shouldn't be needed. So, I took you advice and tried it on a WP site with the extra code. Here's what I get: “video-thumbnaiil.jpg” has failed to upload due to an error Image is small too small. Get something of width more than 350px. – BillK Feb 7 '16 at 0:42
  • That what needed as per the original question. What is you'r problem I don't understand. – Rituparna sonowal Feb 7 '16 at 0:47
  • The code works the way it is and it answers the question. I just noticed there is unnecessary logic with the else statement. All that is neede is if ($width < 350){ $file['error'] = "Image is small too small. Get something of width more than 350px."; return $file; } – BillK Feb 7 '16 at 0:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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