0

I would like to set minimum required size for featured image. I want to prevent using images smaller than the featured image size, because otherwise some images can be displayed improperly.

For example, I set image size

add_image_size('article-retina', 758, 400, true);

but when I chose as featured image file smaller than this size, thumbnails with this size aren't generated so image will not be displayed properly.

So is this can be done simply, or it's more complicated?

2 Answers 2

0

The best solution I can come up with is a two-parter which adds metadata, then filters the images users are allowed to select from.

If I find a way to update the selector to add an alert, I'll add that as a separate answer.

1st: Add image metadata that will allow us to filter images later

add_filter('wp_generate_attachment_metadata', 'wpse_add_meta', 10, 2);
function wpse_add_meta($meta, $id){
    if (array_key_exists('height',$meta)){
        update_post_meta($id, 'height', (int) $meta['height']);
        update_post_meta($id, 'width', (int) $meta['width']);
    }
    return $meta;
}

2d: Filter images returned by the Featured Image selector, using the new meta values for Height and Width (added in filter above).

add_filter('ajax_query_attachments_args', 'e2_attachments_ajax' );
function e2_attachments_ajax($query){
    $minHeight = 400;
    $minWidth = 758;
    $query['meta_query'] = array(
        'relation' => 'AND',
        array(
            'key'     => 'height',
            'value'   => $minHeight,
            'type'    => 'numeric',
            'compare' => '>',
        ),
        array(
            'key'     => 'width',
            'value'   => $minWidth,
            'type'    => 'numeric',
            'compare' => '>',
        )
    );

    return $query;
}

NOTE: For existing sites, you will want to run a process to update existing uploaded images. Run this after the you have added the first filter, "wp_generate_attachment_metadata" so it will update the height and width attributes. I recommend something like Ajax Rebuild Thumbnails.

2
  • Thanks for help! I use both your answers, and now I can't upload smaller size directly to featured image and when I want choose image from my media library I see only those images with min dimienstions. I thinks it is what I need :)
    – Damian
    Sep 25, 2019 at 2:52
  • Unfortunately there is a bug - those minimum sizes are applies to all media, not only to post featured images... so for eg. I can't choose smaller than 758x400px image to insert it to a post... :(
    – Damian
    Sep 30, 2019 at 20:15
2

Add the following to your functions.php file. You can adjust the "height" and "width" dimensions as desired. This will reject uploaded images that fail to meet your minimum dimensions

add_filter('wp_handle_upload_prefilter','wpse_handle_upload_prefilter');
function wpse_handle_upload_prefilter($file)
{

    $image_types = array('png','jpg','jpeg','gif');
    $upload_type = explode('/',$file['type'])[1];
    $minimum = array('width' => '758', 'height' => '400');        

    // only check images...
    if (in_array($upload_type,$image_types)){

        $img = getimagesize($file['tmp_name']);
        $width = $img[0];
        $height = $img[1];

        if ($width < $minimum['width'] ){
            return array("error"=>"The uploaded image is too small. Minimum width is {$minimum['width']}px. The uploaded image width is {$width}px.");    
        } elseif ($height <  $minimum['height']) {
            return array("error"=>"The uploaded image is too small. Minimum height is {$minimum['height']}px. The uploaded image height is {$height}px.");
        } else {
            return $file; 
        } // end if width or height
    } // end if upload type is image type

} // end function
3
  • Ok, but that reject images during upload... I want to reject images smaller than X size during choosing as featured image. So when I choose image 150 x 200px as featured image, then it will be rejected.
    – Damian
    Sep 23, 2019 at 22:16
  • So far, all I find is a way to prevent the user from saving a post after they select a featured image that is too small. Will that work for your needs? Or, do you require something that stops the selection process? Sep 23, 2019 at 22:40
  • For me the best option is alert when I choose too small image as featured, but any way to prevent using to small file is ok :)
    – Damian
    Sep 23, 2019 at 22:43

Your Answer

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

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