Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.