How to Automatically Set the First Image as Featured Image in WordPress (No Plugin Required)

Want to automate setting featured images in WordPress? Learn how to create a custom plugin that sets the first image in each blog post as its featured image—individually or in bulk.

Views: 33

Adding a featured image to every blog post is crucial for design and SEO. But manually setting them can be time-consuming—especially if you already have hundreds of posts.

In this guide, you’ll learn how to create a custom WordPress plugin that automatically sets the first image found in each blog post’s content as the featured image. The plugin adds a menu under Settings, where you can apply the change to individual posts or in bulk—without affecting posts that already have a featured image.


🛠️ Features of This Plugin

  • Lists only blog posts without a featured image
  • Shows a preview of the first image in the post content
  • One-click button to set it as the featured image
  • Option to apply changes in bulk
  • Works seamlessly with the WordPress Media Library
  • Simple UI in the admin dashboard
  • Translation-ready

📁 Step-by-Step Tutorial

Step 1: Create the Plugin Folder and Main File

  1. Open your WordPress site’s /wp-content/plugins/ directory.
  2. Create a new folder called auto-featured-image.
  3. Inside it, create a file named:
    auto-featured-image.php

Step 2: Add Plugin Header and Basic Structure

Paste this code in the auto-featured-image.php file:

<?php
/*
Plugin Name: Auto Featured Image Setter
Description: Sets the first image in post content as the featured image for posts without a featured image.
Version: 1.0
Author: Your Name
*/

Step 3: Register Admin Menu

Use add_submenu_page() to add a new settings page under Settings → Auto Featured Image. This page will show posts missing a featured image.

Step 4: List Posts Without a Featured Image

Use WP_Query with a meta_query checking for _thumbnail_id not existing:

$args = [
'post_type' => 'post',
'meta_query' => [
[
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS',
],
],
];

Loop through results and use preg_match() to extract the first <img> tag from post_content.

Step 5: Display Actions in a Table

Use an admin table to show:

  • Post Title
  • First image preview
  • A “Set Featured” button for single post
  • Checkboxes for bulk action

Step 6: Set Featured Image Programmatically

Use media_sideload_image() and set_post_thumbnail() to download and set the image:

$attachment_id = media_sideload_image($image_url, $post_id, null, 'id');
set_post_thumbnail($post_id, $attachment_id);

📚 References:

Step 7: Add Admin Action Hooks

  • Use admin_post_afi_set_featured_single for handling individual posts.
  • Use admin_post_afi_set_featured_bulk for processing multiple posts at once.

Step 8: Add JavaScript and CSS (Optional)

For better UX, add a JavaScript file:

// js/admin.js
jQuery(function($) {
$('#afi-select-all').on('click', function() {
$('input[name=\"afi_post_ids[]\"]').prop('checked', this.checked);
});
$('#afi-bulk-set').on('click', function() {
return confirm('Are you sure you want to apply this action to all selected posts?');
});
});

Enqueue it using wp_enqueue_script() inside your admin page.

Step 9: Final Touches

  • Use wp_nonce_field() and check_admin_referer() to secure your forms.
  • Wrap all strings with __() or _e() for translation support.

✅ Benefits of This Plugin

  • Saves hours of manual work
  • Keeps your site visually consistent
  • Increases click-through with attractive thumbnails
  • SEO-friendly: Featured images often appear in search previews

💡 Pro Tip

Make this your first “custom utility plugin” and reuse it on every client site that needs featured image automation. You’ll save a ton of time in future projects.


If you have questions or want a downloadable version, drop a comment below or contact me. Happy coding!

Leave a Reply