How to Automatically Set Featured Images for WordPress Posts

In WordPress, a "Featured Image" is an image that represents a post, page, or custom post type. It is frequently used to visually represent content in a variety of places on a website, including blog post lists, homepage sliders, and the post itself when viewed. When a post is shared on a social media platform such as Facebook or Twitter, the featured image is typically displayed alongside the post link, making the content more appealing and clickable.

Typically, to set a featured image in the WordPress post editor, navigate to the "Featured Image" meta box, click "Set featured image," and then select or upload an image from the media library. But what if you frequently forget to set featured images for your posts? Add the below code to your theme's functions.php file. It will set the first image in the post content as the featured image if you already haven't set a featured image for your post.

function themesdna_auto_set_featured_image($post_id) {
    if (wp_is_post_revision($post_id) || has_post_thumbnail($post_id)) {
        return;
    }

    $post = get_post($post_id);
    $content = $post->post_content;

    $matches = [];
    preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $matches);

    if ($matches) {
        $image_url = $matches['src'];
        $image_id = attachment_url_to_postid($image_url);

        if ($image_id) {
            set_post_thumbnail($post_id, $image_id);
        }
    }
}
add_action('save_post', 'themesdna_auto_set_featured_image');

Note:
The above code works with images regardless of whether they are attached to the post, as long as they are embedded in the content. But it will fail if the image URL is from an external source.

Our WordPress Themes

Below are some of our premium WordPress themes. View all our free and premium WordPress themes →
EliteWP PRO WordPress Theme

EliteWP PRO

If you like EliteWP free WordPress Theme, you will love the premium version. EliteWP PRO is an advanced, very user...

$25.00
GridShow PRO WordPress Theme

GridShow PRO

GridShow PRO WordPress theme is a feature-rich, advanced version of GridShow Free WordPress Theme. GridShow PRO version has color and...

$25.00
GridFeel PRO WordPress Theme

GridFeel PRO

$25.00
WP Masonry PRO WordPress Theme

WP Masonry PRO

If you like WP Masonry free WordPress Theme, you will love the premium version. WP Masonry PRO is an improved...

$25.00
TidyMag PRO WordPress Theme

TidyMag PRO

If you like TidyMag free WordPress Theme, you will love the premium version. TidyMag PRO is easy to use, feature-rich,...

$25.00
RapidWP PRO WordPress Theme

RapidWP PRO

Did you try RapidWP Free WordPress Theme?, Then you will love the premium version. RapidWP PRO is an improved, more...

$25.00
GreatWP PRO WordPress Theme

GreatWP PRO

If you like GreatWP free WordPress Theme, you will love the premium version. GreatWP PRO is very user friendly, feature-rich,...

$25.00
GridHot PRO WordPress Theme

GridHot PRO

If you have tried GridHot Free WordPress Theme, then you will love the premium version. GridHot PRO version has color...

$25.00

Can't you choose a single theme? Purchase All Themes for $75.

Save money with our low, one-time price for access to all of our 56 WordPress themes.