Attach image to wordpress RSS feeds using custom fields

Photography was my passion, but got intense as soon as I purchased my first DSLR (Canon 450D). Then I started maintaining my photoBlog.  Now as a next step, I wanted my feeds to have a thumbnail attached to them.  This sounds too easy if in WordPress you have used “insert image” to post (using get_the_post_thumbnail($post->ID) ). But in my case I did not do so.

I am using custom fields (variable name “image”) which points to the URL of the uploaded image. Now I was in a big trouble. Reason being I wanted to be associated with VFXY Photoblogs but they require feeds with thumbnails in it  😆

In order to solve my problem I did some google search and came across few interesting tips. Thanks Nathan!

I will share it here what I did and how I did.

Note: Use this only if you are comfortable editing/modifying your wordpress theme!

Step1:

First and foremost you need to open your functions.php (Login to WordPress -> Appearance -> Editor -> Select functions.php) .

In this file you need to add the below function :

[php]

function rss_post_thumbnail($content) {
global $post;
$cn_image = get_post_meta($post->ID,<span style="color: #ff0000;"><strong> image</strong></span>, true);
if ($cn_image != ”) { $customfeederimage = ‘<img <strong><span style="color: #ff0000;">width="160" height="106"</span></strong> src="’ . $cn_image . ‘" />’; }
if (has_post_thumbnail($post->ID)) { if ($cn_image == ”) { $customfeederimage = get_the_post_thumbnail($post->ID, array(165,165) ); } }
if (!has_post_thumbnail($post->ID) && $cn_image == ” ) { $customfeederimage = ‘<img src="/whatever.jpg" />’;}
$content = ‘<p>’ . $customfeederimage . ‘</p>’ . get_the_excerpt();
return $content;
}
add_filter(‘the_excerpt_rss’, ‘rss_post_thumbnail’);add_filter(‘the_content_feed’, ‘rss_post_thumbnail’);
//add_filter(‘the_content’, ‘rss_post_thumbnail’);

[/php]

In the above function you need to replace the keyword (I am using the keyword image) with the custom field you are using (e.g customimage). Use it without quotes.

Secondly you can specify the height and width of the image you want to attach.

In case you don’t have a custom field, this code will still pick your post thumbnail.

Once edited, save the file and quit. You are almost done!

Step2:

Once you have edited the file, you feeds might not show up with images directly (e.g www.example.com/feed). I had a problem hence I am sharing it here. I use google feedburner (feedburner.google.com) to grab my feeds. So I will suggest to create a feedburner account and register your feeds.

Once registered, you will get a feed URL similar to http://feeds.feedburner.com/AbhizPhotoBlog  (based on what you select as the name of feed).  Now when you click on this feed URL, you will clearly get the images with the feed.

Step 3:

In order to set your feed address to the default of the Google Feedburner, install a plugin “FD Feedburner Plugin”. This plugin is very useful.  Once installed in WordPress, go to Plugins -> Feedburner Configuration and in the field “Redirect my feed URL”, give your Google Feedburner URL address and click Update! This is it!!!!

Now you have a custom field with image added to your RSS feed and also defaulting it to the Feedburner URL.

Please let me know how it works for you. Hope it helps!!

-Abhi