Custom Field Types & Featured Images in WordPress RSS Feed

A client of mine needed to display their featured images/post thumbnails in their RSS Feed. Easy enough, Google to the rescue on that. But while WordPress’ built in all around image uploader/resizer/cropper works great, sometimes the thumbnails are a little lame since you don’t have control over how they get cropped. The client therefore occasionally created their own cropped versions of images and, using a custom field, added them to the post. So moreover than what is explained above, I need a WordPress function that could do the following:

  1. Check to the see if the plugin has a specific custom field value, if so, display that.
  2. If not, check to see if the plugin has a post thumbnail, if so, display that.
  3. If neither of the above are true, display some default image.

If you’d like to implement this, it’s not all that hard, and of course I’ll walk you through it.

  1. Firstly, you’ll need to become familiar with WordPress’ custom fields, and with featured images / post thumbnails.
  2. You’ll be using a custom field for your very custom post thumbnail (the one you’ll create for yourself, if you don’t want to rely on WordPress’ cropping abilities). For the sake of this article, we’ll assume your custom field has a key of custom_thumbnail
  3. Next, copy and paste the following code into your theme’s functions.php file. We’ll discuss the details later:
    function rss_post_thumbnail($content) {
    global $post;
    $cn_image = get_post_meta($post->ID, custom_thumbnail, true);
    if ($cn_image != '') { $customfeederimage = '<img 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="https://48jwh53mcwj217jvx11dcwjj-wpengine.netdna-ssl.com/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');
  4. Save your functions.php file and you’re good to go, after you make any necessary modifications (as outlined below).

Customizing the Code

Here we’ll analyze the important, customizable pieces of the code. In this first area, we’re setting our $cn_image variable to be the value of our custom field with the key custom_thumbnail. If your custom field has a different key, use that instead.

$cn_image = get_post_meta($post->ID, custom_thumbnail, true);

Next we’ll check to see if there was a custom field value. If so, set our $customfeederimage variable to equal an image tag with the src value being our custom image.

if ($cn_image != '') { $customfeederimage = '<img src="' . $cn_image . '" />'; }

Next we’ll say, okay, check to see if the post has a post thumbnail. If so, and if there was no custom field value, make our $customfeederimage equal the post thumbnail. Also note the 165,165. That is where I’m specifically setting the width and height of the thumbnail. You can remove the array(165,165) altogether and use the default ‘thumbnail’ value, or any others from get_the_post_thumbnail.

if (has_post_thumbnail($post->ID)) { if ($cn_image == '') { $customfeederimage = get_the_post_thumbnail($post->ID, array(165,165) ); } }

And finally, if there was no post thumbnail and the custom field was blank, display a default image.

if (!has_post_thumbnail($post->ID) && $cn_image == '' ) { $customfeederimage = '<img src="https://48jwh53mcwj217jvx11dcwjj-wpengine.netdna-ssl.com/whatever.jpg" />';}

Finally, you can switch out get_the_excerpt() with get_the_content() if you’d rather display your feed’s entire content vs. just an excerpt (and I would recommend this!)

$content = '<p>' . $customfeederimage . '</p>' . get_the_excerpt();

Up Next: Yes, I'll take the Full Content with my RSS, Please