How to set a default featured image in WordPress posts (easy)

default-featured-image-in-wordpress

How to set a default featured image in WordPress posts? Let’s see together an easy way, without any plugins.

Table of contents

Featured images (also sometimes called Post Thumbnails) are images that represent an individual Post, Page, or Custom Post Type. Usually, you need to use them on your custom theme when showing the posts. Also, the featured image of the post is used for social media as open graph image (og:image).

In some situations, it may be not possible to upload an image for your post. One reason could be that you haven’t the right image yet, or your designer hasn’t created it yet.

Another reason, if you are an author of a news portal site, is that you may need to post a breaking post as soon as possible. In this case, the only thing that matters to you is to upload the information immediately before your competitors from other news portals do it.

Let’s see how you can do it without any plugins.

In order to set our default image for WordPress posts, you need to write some PHP code in the theme’s functions.php file. The hook you need to use depends on what editor you are using.

The function that you need to set the thumbnail to the post is the set_post_thumbnail, which takes as arguments the post object or post ID and the thumbnail ID. You have the post object / ID from the hook, but you need to get the thumbnail ID also.

The idea is to upload the image in the WordPress media library and then use its ID to set it as a thumbnail of the post.

How to get the image ID

WordPress handles images as attachment post types, so they have an ID in the database. To get the image ID either open it from the media library and get it from the URL, from the item param

WordPress image ID

or get the image URL, and then use the attachment_url_to_postid function to get the ID

WordPress image URL

Block editor

If you use the block editor you need to use the rest_after_insert_post hook in your functions.php file:

function mytheme_set_default_thumbnail( $post ) {
    if ( $post->post_type != 'post' ) {
        return;
    }

    if ( !has_post_thumbnail( $post->ID ) ) {
        $thumbnail_url = 'https://test.local/wp-content/uploads/2022/10/dev3.jpg';
        $thumbnail_id = attachment_url_to_postid( $thumbnail_url );

        set_post_thumbnail( $post->ID, $thumbnail_id );
    }
}
add_action( 'rest_after_insert_post', 'mytheme_set_default_thumbnail', 10, 3 );

Classic editor

If you use the classic editor you need to use the save_post hook in your functions.php file:

function mytheme_set_default_thumbnail( $post_id ) {
    global $post;

    if ( $post->post_type != 'post' ) {
        return;
    }

    if ( !has_post_thumbnail( $post_id ) ) {
        $thumbnail_url = 'https://test.local/wp-content/uploads/2022/10/dev3.jpg';
        $thumbnail_id = attachment_url_to_postid( $thumbnail_url );

        set_post_thumbnail( $post_id, $thumbnail_id );
    }    
}
add_action( 'save_post', 'mytheme_set_default_thumbnail' );

If you don’t want to get the image ID by URL, you can hardcode the ID, but in this case, you need to make sure that the image exists using the wp_get_attachment_url function:

if ( !has_post_thumbnail( $post_id ) ) {	
	$thumbnail_id = 40;

	// check if thumbnail exists
	if ( wp_get_attachment_url( $thumbnail_id) != false ) {
		set_post_thumbnail( $post_id, $thumbnail_id );
	}
}

In addition, you may notice that we check the post type at the beginning of the function. You can run this code for any post type you need, for example:

if ( $post->post_type != 'post' && $post->post_type != 'book' ) {
    return;
}

Assume that book is a custom post type.

The image should not be deleted from the WordPress media library


Summary

Let’s summarize the steps you need to follow for setting a default featured image in WordPress posts:

  1. Upload the image to the WordPress media
  2. Use the rest_after_insert_post or the save_post hook
  3. Check if the post has a thumbnail already using the has_post_thumbnail function
  4. Get the thumbnail ID
  5. Set the post thumbnail using the set_post_thumbnail function


I hope that you found this article helpful. Remember that WordPress provides very useful functions and hooks to make our lives easier.

More Articles

All Articles
All Articles
© Antonis Papadakis 2024. All rights reserved.