Last updated on September 24, 2023

Add a Featured Image to the RSS Feed

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Boost visibility with featured RSS images.

To add a featured image to the RSS feed in WordPress, you can use the the_excerpt_rss filter hook along with the get_the_post_thumbnail_url function. This will allow you to include the featured image URL in the RSS feed.

Here’s an example code snippet that you can add to your theme’s functions.php file:

function wpsnippets_add_featured_image_to_rss($content) {
    global $post;

    if (has_post_thumbnail($post->ID)) {
        $featured_image_url = get_the_post_thumbnail_url($post->ID, 'full');
        $content = '<p><img src="' . $featured_image_url . '" /></p>' . $content;
    }

    return $content;
}
add_filter('the_excerpt_rss', 'wpsnippets_add_featured_image_to_rss');

This code snippet adds the featured image URL as an <img> tag before the content of each post in the RSS feed. The has_post_thumbnail function checks if the post has a featured image, and the get_the_post_thumbnail_url function retrieves the URL of the featured image. The URL is then appended to the content using the $content variable.

By adding this code snippet, you can enhance your RSS feed by including the featured image of each post, which can be useful for displaying images in RSS readers or other applications that consume your feed.

Examples

Example 1: Add Featured Image to RSS Feed

This use case demonstrates how to add the featured image of a post to the RSS feed. By default, WordPress does not include the featured image in the RSS feed, but with a simple code snippet, you can add this functionality.

function wpsnippets_add_featured_image_to_rss_feed($content) {
    global $post;
    if (has_post_thumbnail($post->ID)) {
        $content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . $content;
    }
    return $content;
}
add_filter('the_excerpt_rss', 'wpsnippets_add_featured_image_to_rss_feed');
add_filter('the_content_feed', 'wpsnippets_add_featured_image_to_rss_feed');

This code snippet adds a filter to the the_excerpt_rss and the_content_feed hooks, which are responsible for generating the RSS feed content. The wpsnippets_add_featured_image_to_rss_feed function checks if the post has a featured image using has_post_thumbnail(), and if so, it prepends the featured image to the content.

Example 2: Customize Featured Image Size in RSS Feed

This use case demonstrates how to customize the size of the featured image displayed in the RSS feed. By default, the size of the featured image in the RSS feed is determined by the theme’s settings. However, you can override this and specify a custom size using the the_post_thumbnail_size filter.

function wpsnippets_customize_featured_image_size($size) {
    return array(300, 200); // Custom size: 300px width, 200px height
}
add_filter('the_post_thumbnail_size', 'wpsnippets_customize_featured_image_size');

This code snippet adds a filter to the the_post_thumbnail_size hook, which allows you to customize the size of the featured image. In this example, the wpsnippets_customize_featured_image_size function returns an array with the desired width and height of the featured image.

Example 3: Exclude Featured Image from RSS Feed

This use case demonstrates how to exclude the featured image from the RSS feed. Sometimes, you may want to exclude the featured image from the RSS feed for specific posts or post types. You can achieve this by modifying the wpsnippets_add_featured_image_to_rss_feed function from Example 1.

function wpsnippets_exclude_featured_image_from_rss_feed($content) {
    global $post;
    if (has_post_thumbnail($post->ID)) {
        $content = str_replace(get_the_post_thumbnail($post->ID), '', $content);
    }
    return $content;
}
add_filter('the_excerpt_rss', 'wpsnippets_exclude_featured_image_from_rss_feed');
add_filter('the_content_feed', 'wpsnippets_exclude_featured_image_from_rss_feed');

This code snippet modifies the wpsnippets_add_featured_image_to_rss_feed function from Example 1. Instead of prepending the featured image to the content, it uses str_replace() to remove the featured image from the content. This effectively excludes the featured image from the RSS feed.

Last updated on September 24, 2023. Originally posted on September 24, 2023.

Leave a Reply

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