How to Set a Default Featured Image to posts in WordPress

How to Set a Default Featured Image to posts in WordPress

This Blog post will discuss that how to set a default featured image to post in WordPress. By post we are referring to even pages and also custom post types. We will explain both ways of doing it i-e with the use of a plugin and with custom code as well so let’s get started.

1. By Using Plugin.

There are some plugins from which you can set a default featured image to posts. e-g “Default Featured Image” When you have installed the plugin you can navigate to dashboard->settings->media in that page and you will see an option to set a default image. You can upload the image to the media and set it up as default post image.

2. Programmatically with custom code snippet

In case you don’t want to go with the plugins solution than you can use a PHP code snippet and add it to set the default post image. here is how you can do that.

function wpexpertguide_set_default_featured_image( $post_id, $post ) {
 // Check if the post is being published and does not have a featured image set.
 if ( 'publish' === $post->post_status && ! has_post_thumbnail( $post_id ) ) {
 $default_image_id = 1234; // Replace with the attachment ID of your default image.
 // Set the default image as the featured image.
 set_post_thumbnail( $post_id, $default_image_id );
 }
 return $post_id;
}
add_action( 'save_post', 'wpexpertguide_set_default_featured_image', 10, 2 );

The function is hooked into the save_post action, and it triggers whenever a post is saved or updated. The function receives two parameters the post ID and the post object. Inside the function we first check that if the post is being published and doesn’t have a featured image set then we can set our default image with image attachment id. If the image is already uploaded to the media library you can find its attachment ID by going to the Media Library in your WordPress admin dashboard and clicking on the image. In the URL of the image, you’ll find the attachment ID parameter.

And If you only want to set the default featured image for a specific post type you can do that also.

function wpexpertguide_set_default_featured_image( $post_id, $post ) {
 // Define an array of post types where you want to set the default image.
 $allowed_post_types = array( 'post', 'your_custom_post_type' ); // Replace 'your_custom_post_type' with the actual custom post type slug.
 if ( 'publish' === $post->post_status && in_array( $post->post_type, $allowed_post_types, true ) && ! has_post_thumbnail( $post_id ) ) {
 $default_image_id = 1234;
 set_post_thumbnail( $post_id, $default_image_id );
 }
 return $post_id;
}
add_action( 'save_post', 'wpexpertguide_set_default_featured_image', 10, 2 );

There is also another way around instead of setting a default image so what you can do is check if a post has no featured image set only than add a default image. Here is how you can do that.

function wpexpertguide_my_filter_thumbnail_id( $thumbnail_id, $post = null ) {
    if ( ! $thumbnail_id ) {
            $thumbnail_id = 1234; //id of default featured image
        }
        return  $thumbnail_id;
    }
    
 add_filter( 'post_thumbnail_id', 'wpexpertguide_my_filter_thumbnail_id', 20, 5 );

You can also add a if condition of your post type like if ($post->post_type == 'post').

So now you have successfully learned how to add a default featured image to custom post types, posts, and pages in WordPress. By following the steps in this tutorial and implementing the provided code snippets in your child theme’s functions.php file you can now display a default image whenever a featured image is not set.

Leave a Reply

Your email address will not be published. Required fields are marked *