Halloween Sale 15% off Ends in See the plans
PHP

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.

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

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

  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.