Block image uploads below 600 px wide or above 6000 px on either side, with a clear message explaining why.
add_filter( 'wp_handle_upload_prefilter', function ( array $file ): array {
if ( ! str_starts_with( (string) ( $file['type'] ?? '' ), 'image/' ) || 'image/svg+xml' === $file['type'] ) {
return $file;
}
$size = @getimagesize( $file['tmp_name'] );
if ( ! $size ) {
return $file;
}
[ $width, $height ] = $size;
if ( $width < 600 ) {
$file['error'] = sprintf( 'This image is %d px wide. Please upload one at least 600 px wide.', $width );
} elseif ( $width > 6000 || $height > 6000 ) {
$file['error'] = sprintf( 'This image is %d × %d px. Please resize it to 6000 px or less on each side.', $width, $height );
}
return $file;
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Tiny images look blurry when your theme stretches them, and huge camera originals can run the server out of memory while WordPress creates thumbnails. This snippet stops both at upload time.
wp_handle_upload_prefilter runs before an uploaded file is saved. For images, the snippet reads the width and height with getimagesize(). If they’re outside your limits, it sets $file['error'], and WordPress shows that message in the uploader and refuses the file.
SVGs and files that aren’t images are skipped.
current_user_can( 'manage_options' ) to the first check to let admins upload anything.Upload an image 400 px wide. You should see the “at least 600 px wide” message and the file shouldn’t be added.