Minimum Size for Featured Images
Stops posts from being published with a featured image smaller than the size you set, such as 1200 by 630 pixels, so social previews and hero images never look blurry.
Opens WPIDE on
Code
// Minimum featured image size in pixels.
$wpide_min_thumb_width = 1200;
$wpide_min_thumb_height = 630;
add_filter( 'rest_pre_insert_post', function ( $prepared, $request ) use ( $wpide_min_thumb_width, $wpide_min_thumb_height ) {
$post_id = (int) ( $prepared->ID ?? 0 );
$status = $request->get_param( 'status' ) ? $request->get_param( 'status' ) : get_post_status( $post_id );
if ( ! in_array( $status, array( 'publish', 'future' ), true ) ) {
return $prepared;
}
$image_id = null !== $request->get_param( 'featured_media' ) ? (int) $request->get_param( 'featured_media' ) : (int) get_post_thumbnail_id( $post_id );
if ( ! $image_id ) {
return $prepared;
}
$meta = wp_get_attachment_metadata( $image_id );
if ( empty( $meta['width'] ) || ( $meta['width'] >= $wpide_min_thumb_width && $meta['height'] >= $wpide_min_thumb_height ) ) {
return $prepared;
}
return new WP_Error(
'wpide_featured_image_too_small',
sprintf( 'The featured image is %1$d x %2$d pixels; please use one of at least %3$d x %4$d.', $meta['width'], $meta['height'], $wpide_min_thumb_width, $wpide_min_thumb_height ),
array( 'status' => 400 )
);
}, 10, 2 );
Notes
Posts without a featured image are not affected; pair it with a "require featured image" rule if you need both.
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.