Minimum Image Size for an ACF Image Field
Rejects uploads to a chosen ACF image field when the picture is smaller than the width and height you need, such as for hero banners.
Opens WPIDE on
Code
// Field name and minimum size in pixels.
$wpide_acf_image_field = 'hero_image';
$wpide_acf_min_width = 1600;
$wpide_acf_min_height = 600;
add_filter( 'acf/upload_prefilter/name=' . $wpide_acf_image_field, function ( $errors, $file ) use ( $wpide_acf_min_width, $wpide_acf_min_height ) {
$size = ! empty( $file['tmp_name'] ) ? wp_getimagesize( $file['tmp_name'] ) : false;
if ( $size && ( $size[0] < $wpide_acf_min_width || $size[1] < $wpide_acf_min_height ) ) {
$errors['min_dimensions'] = sprintf( 'Images must be at least %1$d × %2$d pixels; this one is %3$d × %4$d.', $wpide_acf_min_width, $wpide_acf_min_height, $size[0], $size[1] );
}
return $errors;
}, 10, 2 );
Notes
Applies to new uploads through the field. ACF also has min width and height settings for images picked from the library.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.