How to Optimize RSS to Display Featured Blog Images

March 14, 2012
wordpress-blog-expert-series

Images are playing a bigger part of blogging, with themes that highlight the posts’ featured images becoming more popular, creating great visually appealing layouts. Even more so with the popularity of Pinterest and Facebook sharing, which uses an image from the page or post. Even with various methods for including specific Facebook OpenGraph code within your post, your content could be shared via other methods that don’t take the Opengraph information into account, like RSS and feed syndication or sharing services like the awesome DLVR.it or Feedburner.

UPDATE AUGUST 2013: I went ahead and created a WordPress plugin for this, since there were quite a few people not having success with editing their theme files. Please read the new post instead of trying to follow the more technical code modifications below!

Click here: WordPress Featured Images in RSS Feed plugin

Here’s a fairly simple way to include the featured image of your WordPress blog post in the RSS/Feedburner feed itself, so that services that syndicate or share via RSS, use it when posting to Facebook and other image-friendly sharing sites like tumblr or posterous, etc.

Edit your theme’s functions.php file (via FTP, your host’s File Manager, or WordPress’ Editor under Appearance) which is under the /wp-content/themes/themename/ folder, or create one if need be, and add these lines near the top:

function featuredtoRSS($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail', array( 'style' => 'float:left; margin:0 15px 15px 0;' ) ) . '' . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'featuredtoRSS');
add_filter('the_content_feed', 'featuredtoRSS');

To display larger images instead, change the word ‘thumbnail‘ to ‘medium‘, ‘large‘ or ‘full‘. To remove the float so the image appears above your text, replace the content line above with this one:

$content = '<div>' . get_the_post_thumbnail( $post->ID, 'full', array( 'style' => 'margin-bottom: 15px;') ) . '</div>'. $content;

You should be able to preview your RSS feed using your favorite feed reader, but keep in mind services like Feedburner cache your feed, so expect to wait 12-24 hours for it to update before you see these changes.

For more WordPress support check out these helpful articles from our WordPress Expert Series:

Why the 1-Click WordPress Install Should Be Avoided

WordPress for Enterprise Facts, Myths, and Tips for Success

3 Surprising WordPress Uses Beyond Blogging for Business

How to Set Up 301 Redirects to Maintain WordPress SEO

Share

You Might Also Like