To Change post’s default placeholder image to custom image Use Below Code
Add code to your child theme’s functions.php
file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php
file as this will be wiped entirely when you update the theme.
/**
* Change placeholder image for all post (globally)
*/
function custom_default_post_thumbnail($html, $post_id, $post_thumbnail_id, $size, $attr) {
if (empty($html)) {
$default_image_url = get_stylesheet_directory_uri() . '/assets/custom-placeholder.webp'; // replace with your image path
$html = '<img src="' . esc_url($default_image_url) . '" class="wp-post-image" alt="Default image" />';
}
return $html;
}
add_filter('post_thumbnail_html', 'custom_default_post_thumbnail', 10, 5);
in above code image is store in child theme’s assets directory.
This code will:
- Check if a post has a featured image.
- If not, it will display your custom default image.
You can use media upload also
/**
* Change placeholder image for all post (globally)
*/
function custom_default_post_thumbnail($html, $post_id, $post_thumbnail_id, $size, $attr) {
if (empty($html)) {
$default_image_url = 'https://your-site.com/wp-content/uploads/2025/04/product--custom-placeholder.webp'; // replace with your image URL
$html = '<img src="' . esc_url($default_image_url) . '" class="wp-post-image" alt="Default image" />';
}
return $html;
}
add_filter('post_thumbnail_html', 'custom_default_post_thumbnail', 10, 5);
Serve Different Placeholder Images Based on Post Category or Post Type
function dynamic_placeholder_by_category($html, $post_id, $post_thumbnail_id, $size, $attr) {
if (empty($html)) {
$category = get_the_category($post_id);
$default = 'https://your-site.com/default.jpg';
$tech_placeholder = 'https://your-site.com/tech.jpg';
$lifestyle_placeholder = 'https://your-site.com/lifestyle.jpg';
$image = $default;
if (!empty($category)) {
if ($category[0]->slug == 'tech') {
$image = $tech_placeholder;
} elseif ($category[0]->slug == 'lifestyle') {
$image = $lifestyle_placeholder;
}
}
$html = '<img src="' . esc_url($image) . '" class="wp-post-image" alt="Default image" />';
}
return $html;
}
add_filter('post_thumbnail_html', 'dynamic_placeholder_by_category', 10, 5);
✅ Great for multi-niche blogs to keep the design more relevant and dynamic.
Use Theme Support for get_the_post_thumbnail_url()
Fallback
Instead of filtering the HTML, filter the thumbnail URL directly:
function custom_get_post_thumbnail_url($url, $post, $size) {
if (!$url) {
$url = 'https://your-site.com/wp-content/uploads/2025/04/product--custom-placeholder.webp'; // your placeholder
}
return $url;
}
add_filter('post_thumbnail_url', 'custom_get_post_thumbnail_url', 10, 3);
✅ Use this when you want finer control over lazy loading, image sizes, or AMP compatibility.
🧠 Pro Tips for Readers
- Optimize Placeholder Size – Use low-resolution or SVG placeholders for performance.
- CDN Integration – Store and serve fallback from a CDN for faster delivery.