Minimum and Maximum Image Dimensions for Uploads
Rejects image uploads that are smaller or larger than the pixel sizes you set, so blurry thumbnails and oversized camera originals never reach the Media Library.
Opens WPIDE on
Code
// Limits in pixels (0 = no limit).
$wpide_min_width = 300;
$wpide_min_height = 0;
$wpide_max_width = 8000;
$wpide_max_height = 8000;
add_filter( 'wp_handle_upload_prefilter', function ( $file ) use ( $wpide_min_width, $wpide_min_height, $wpide_max_width, $wpide_max_height ) {
if ( ! empty( $file['error'] ) || empty( $file['type'] ) || 0 !== strpos( $file['type'], 'image/' ) || 'image/svg+xml' === $file['type'] ) {
return $file;
}
$size = wp_getimagesize( $file['tmp_name'] );
if ( ! $size ) {
return $file;
}
list( $width, $height ) = $size;
if ( ( $wpide_min_width && $width < $wpide_min_width ) || ( $wpide_min_height && $height < $wpide_min_height ) ) {
$file['error'] = sprintf( 'This image is %1$d x %2$d pixels. Images must be at least %3$d x %4$d pixels.', $width, $height, $wpide_min_width, $wpide_min_height );
} elseif ( ( $wpide_max_width && $width > $wpide_max_width ) || ( $wpide_max_height && $height > $wpide_max_height ) ) {
$file['error'] = sprintf( 'This image is %1$d x %2$d pixels. Images can be at most %3$d x %4$d pixels.', $width, $height, $wpide_max_width, $wpide_max_height );
}
return $file;
} );
Notes
Set any limit to 0 to switch it off. SVG files are not checked.
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.