How to include the image in your WordPress RSS feed

 

dogs queuing for pee

Cut the queue if you know how.

WordPress is a great open source project for blogging and content management. Millions of users use it everyday. RSS (Really Simple Syndication) feed is a very commonly used feature among bloggers to broadcast latest posts and updates.

Unfortunately, the default WordPress RSS does not include any media items. Some bloggers said the image URL will be included if you choose Full Text under Settings—Reading–For each article in a feed, show. Unfortunately this is not true for the default templates (Twentyten or Twentyeleven).

There are different ways to include images in the RSS feed. You can directly modify the feed-rss2.php under wp-includes directory.

The safer way to modify the default RSS feed behavior is through your template. This will avoid some possible issues in future upgrade. So, here is how to put the URL of the first image in your RSS feed.

Corresponding to your theme (template), there is a file named functions.php, in which most template related functions (e.g., rendering comments) are placed. So, you can open this file with any text editor (e.g., Notepad++), and scroll all the way to the end, and put the following code:
function add_image_into_rss_feeds($content) {
global $post, $posts;
$img_url_to_be_inserted = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$img_url_to_be_inserted = $matches [1] [0];
if(!empty($img_url_to_be_inserted)){
$content = '<img src="' . $img_url_to_be_inserted . '">' . $content;
}
return $content;
}
// if you want to include image when using excerts, uncomment the following line
// add_filter('the_excerpt_rss', 'add_image_into_rss_feeds');
add_filter('the_content_feed', 'add_image_into_rss_feeds');

Now, add a new post with an image, then generate the new RSS feed. You images should show up properly. A working example is a funny picture blog. You may check the feed here: http://feeds.feedburner.com/JokeslabMedia

Comments

  1. What do you mean ‘generate the new RSS feed’
    Does this go on the blog that the feed comes from, or the blog that’s importing the feed????

Leave a Reply