SEO PHP Beginner

Add Featured Images to the RSS Feed

Include each post's featured image at the top of its RSS feed item, so feed readers and email newsletters show it.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 9 lines
function nahnu_feed_featured_image( string $content ): string {
	if ( has_post_thumbnail() ) {
		$image   = get_the_post_thumbnail( null, 'large', [ 'style' => 'max-width:100%;height:auto;' ] );
		$content = '<p>' . $image . '</p>' . $content;
	}
	return $content;
}
add_filter( 'the_excerpt_rss', 'nahnu_feed_featured_image' );
add_filter( 'the_content_feed', 'nahnu_feed_featured_image' );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress’s RSS feed doesn’t include featured images, so feed readers and RSS-to-email newsletters show text only. This snippet adds the image to the start of each item.

How it works

WordPress filters feed content through the_content_feed and summaries through the_excerpt_rss. The snippet adds the featured image, at the large size with an inline style that keeps it within the reader’s width, to the start of both.

Things to watch

  • Inline styles are used because feed readers ignore your theme’s CSS.
  • Feed readers cache items. Changes appear on new posts or after the reader refreshes.

How to test it

Open yoursite.com/feed/ and check that items with a featured image include an &lt;img&gt; tag.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only