How to Add a Default Featured Image to Custom Post Types, Posts, and Pages in WordPress
In this tutorial, we will learn how to add a default featured image to custom post types, regular posts, and pages in WordPress. By default, WordPress allows you to set a featured image for posts and pages, but it does not provide a built-in option to set a default image. We will accomplish this by using a child theme and implementing a custom function. By the end of this tutorial, you will be able to enhance the visual appeal of your website by automatically displaying a default image whenever a post, page, or custom post type does not have a featured image set.
Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.
Add the default featured image function
To get started, locate and open the functions.php
file of your WordPress child theme. Open the functions.php
file in your child theme and insert the following code snippet:
/**
* Set a default featured image for posts, pages, and custom post types.
*/
function 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 = 4665; // 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', 'set_default_featured_image', 10, 2 );
Code language: PHP (php)
Explanation:
- The
set_default_featured_image
function is responsible for setting a default featured image for posts, pages, and custom post types. - The function is hooked into the
save_post
action, which 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 if the post is being published and doesn’t have a featured image set by using the condition
$post->post_status === 'publish' && ! has_post_thumbnail( $post_id )
. - If the post is being published without a featured image, we retrieve the attachment ID of the default image and store it in the
$default_image_id
variable. - Finally, we use the
set_post_thumbnail
function to set the default image as the featured image for the post.
Replace the Attachment ID
Replace YOUR_DEFAULT_IMAGE_ATTACHMENT_ID
in the code snippet with the attachment ID of your default image. 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 (attachment_id=123
).
Set Default Image for Specific Post Types
If you want to set the default featured image only for specific post types, you can modify the set_default_featured_image
function to include a condition that checks the post type. Here’s how you can do it:
/**
* Set a default featured image for posts and specific custom post types.
*/
function 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.
// Check if the post is being published, belongs to an allowed post type, and does not have a featured image set.
if ( 'publish' === $post->post_status && in_array( $post->post_type, $allowed_post_types, true ) && ! has_post_thumbnail( $post_id ) ) {
$default_image_id = 4665; // 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', 'set_default_featured_image', 10, 2 );
Code language: PHP (php)
In this code, replace 'your_custom_post_type'
with the actual slug of the custom post type for which you want to set the default featured image.
Save and upload changes
After replacing the attachment ID, save the functions.php
file. Upload the modified file to the child theme directory on your WordPress server.
Test the default image
Visit your website and create a new post, page, or custom post type without setting a featured image. You should now see the default image displayed in place of the missing featured image.
Previewing and Debugging the Changes
To see the changes in action, you can preview a post or page without a featured image. You’ll notice that the default image is now set as the featured image. Additionally, you can use various debugging tools provided by social media platforms to preview how your posts will appear when shared.
- Facebook Debugger:
- Visit the Facebook Debugger tool.
- Enter the URL of the post or page you want to preview in the input field.
- Click the “Debug” button to fetch the latest information about the shared content.
- Facebook Debugger will display the preview of your post, including the featured image. If the default image is set correctly, you should see it in the preview.
- LinkedIn Debugger:
- Access the LinkedIn Post Inspector tool.
- Paste the URL of your post or page into the provided input field.
- Click the “Inspect” button to analyze the shared content.
- LinkedIn Debugger will show a preview of your post, including the featured image. Verify that the default image is correctly displayed in the preview.
Using these debugging tools, you can ensure that the default image is set up properly and visible when your content is shared on social media platforms. Keep in mind that each platform may have its own debugger tool or validation process, so adjust your testing accordingly if you are sharing content on other platforms.
That’s it! You have successfully learned how to add a default featured image to custom post types, posts, and pages in WordPress without relying on plugins. 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 your feedback and help us improve 🐶
We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.
2 Comments
Thanks for this, it works great but with one big glitch.. when first publishing a post WITH a featured image, the default image will replace the featured image when first saved/published. You need to then go back in and edit the post again a second time to replace the default featured image with the real featured image.
I believe this is because, when first creating the post and before any save_post function has been run, the code is checking for the featured image before one could ever exist.. in other words, checking to see if ( ! has_post_thumbnail( $post_id ) ) will always return false before any post data is ever created for a new post.
Maybe an easy fix??
Hi Ryan, you’re correct in your observation. The behavior you’re experiencing is due to the fact that when you initially create a new post, there’s technically no post data saved yet, so the check for the featured image (
has_post_thumbnail( $post_id )
) will always return false. Therefore, the default image gets set even when you intend to have a different featured image.To address this issue and ensure that the default image is only set if the post is being published without a featured image intentionally, you could modify the function logic slightly. One approach is to check whether the post status is “publish” and whether the post thumbnail has been explicitly set. Here’s how you can modify the
set_default_featured_image
function to achieve this:By adding the condition
$post->post_status === 'publish'
before checking for the featured image, you ensure that the default image is only set when the post is being published and doesn’t have a featured image set. This should help you avoid the issue of the default image replacing the featured image when first publishing a post.Give this modification a try and see if it resolves the glitch you’ve mentioned.