How to Set Featured Image in WordPress Posts Automatically

The Featured Image (also known as post thumbnail) is an important feature of WordPress theme. Featured Image allows theme developers to add representative image for posts, pages, or custom post types. Feature image enhances the presentation of your site and promotes blog posts on social media. It is a good practice to always set the feature image for your posts. To make sure feature image is inserted in every posts, you can make settings to automatically insert feature image even if it is not defined. When it was introduced in WordPress version 2.9, it was named as Post thumbnail. But in later versions it was renamed to Post Featured Image. The two names can be used interchangeably. Post Featured Image is the main identity image of your entire post. Featured image makes your site look professional and help to catch attention of viewers. So how to set featured image in WordPress posts automatically.

How to Set Featured Image in WordPress Posts Automatically

It is easy to forget to set a featured image. However it is a good practice to set featured image for all of your post. SEO plugins and theme may need the featured image for rich snippets and social sites. Lot of the themes has built in support for thumbnails.

The first thing you need to do is test weather your theme supports featured image or not. The easiest way to check is by going to any new post from post editor in the dashboard. When you scroll down you should see a meta box called feature image in the  right side of the screen as shown below.

How to Set Featured Image in WordPress Posts Automatically

If you don’t find the feature image box do not worry, just add few lines of code in your function.php file to activate the feature image. To do so, go to Appearance >> Editor.

How to Set Featured Image in WordPress Posts Automatically

This will open the editor page. On the right side in the  themes folder find and open the file with name functions.php.

How to Set Featured Image in WordPress Posts Automatically

Inside the file copy and paste the following code inside php tag.

add_theme_support(‘post-thumbnails’);

After you click the update button you should see the feature image meta box in the post adding section. However to display the feature image in the website you still have to add the following code to the place where you need the image.

the_post_thumbnail();

Similarly if you want to define the size of post thumbnail. You can add the following code in Functions.php file.

set_post_thumbnail_size(50, 50);
Setting Featured Thumbnails Automatically to WordPress posts

Since feature image is a important element of a post  it is a good practice to regularly insert the feature image. Since it is easy to forget to put the feature image you can insert the following code inside the function.php file to automate the task.

<?php
function auto_featured_thumb() {
  global $post;
  $has_thumb = has_post_thumbnail($post->ID);
  if (!$has_thumb)  {
    $attached_image = get_children('post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1');
    if ($attached_image) {
      foreach ($attached_image as $attachment_id => $attachment) {
        set_post_thumbnail($post->ID, $attachment_id);
      }
    }
  }
}
add_action('the_post', 'auto_featured_thumb');
add_action('save_post', 'auto_featured_thumb');
add_action('draft_to_publish', 'auto_featured_thumb');
add_action('new_to_publish', 'auto_featured_thumb');
add_action('pending_to_publish', 'auto_featured_thumb');
add_action('future_to_publish', 'auto_featured_thumb');
?>

The code first of all checks if thumbnail is set or not. If the thumbnail is set, it skips its further action. But if thumbnail is not set it uses the first image of the post as the feature image.

Also Read:   Create Contact Form Using Formidable Forms in WordPress
Using Plugin to Set Featured Image

Surely one more safe method to set  feature image automatically is by using the plugins like  Auto Post Thumbnail. Auto Post Thumbnail is a wonderful plugin that can automatically generate the post thumbnail from the first image in post or any custom post types if post thumbnail is not set. If first image do not work it will automatically search for next and so on. To install the plugin login to admin dashboard and navigate to Plugins >> Add New

How to Set Featured Image in WordPress Posts Automatically

Now search for  Auto Post Thumbnail in the search field and install the plugin with the similar icon as shown in picture.

How to Set Featured Image in WordPress Posts Automatically

After the installation don’t forget to activate it. Now to generate thumbnail go to Settings >> Auto Post Thumbnail and click Generate Thumbnails button.

How to Set Featured Image in WordPress Posts Automatically

The plugin will start generating post thumbnails automatically.

How to Set Featured Image in WordPress Posts Automatically

Featured image (Post Thumbnail ) is a important element of WordPress posts and blogs. Now with these techniques you can be sure that non of your posts lacks featured image.

The following two tabs change content below.

Kantiman Bajracharya

Kantiman Bajracharya is a freelance web developer. He is a computer geek who has a Bachelor’s degree in Computer Engineering. He has earned lots of satisfied customers while working for numerous successful projects. He is also a WordPress theme creator and web article writer. On free time he likes reading philosophy books, traveling and playing chess.

Share This Post