Adding Featured Images to WordPress feeds

In WordPress and other CMSs, a Featured Image is an image used to represent an article. Ideally, it’s unique so that each article has its own. How a Featured Image appears on a WordPress blog depends largely on the theme being used. Some themes don’t support them, while others do. How a Featured Image appears in a feed however, is a different story.

WordPress doesn’t show Featured Images in feeds by default. It will show images that are parts of articles, but not Featured Images. To get WordPress to show those, we have to do a little coding. Specifically, we want to add a few lines of code to the following files:

wp-includes/
    feed-atom.php
    feed-rdf.php
    feed-rss.php
    feed-rss2.php

As far as I know it isn’t possible to edit these file within WordPress. They’re not part of a theme or plugin, so it’s not possible to edit them with the Theme Editor or Plugin Editor. If a person has access to a website’s code, they’ll need a text editor in order to copy-and-paste the code snippets below.

Each of the snippets relies on the get_the_post_thumbnail_url() function built into WordPress. Simply put, if the post has a thumbnail (a/k/a a Featured Image) associated with it, the snippet will add it to the feed. For the RDF, RSS, and RSS2 snippets, the code is the same, but the placement differs. Atom does things differently all around.

feed-atom.php

For each snippet, the goal is to make the Featured Image the first part of the entry for each post. In feed-atom.php, scroll down to the line that reads <entry>. Place the cursor just after the > and press the Enter key three times. Press the up arrow key to go to the middle of the blank lines, then copy the snippet below and paste it onto this line:

<?php 
    if (has_post_thumbnail($post->ID)){
        echo '<content src="'.get_the_post_thumbnail_url($post->ID).'"/>';
    } 
?>

Save the file and it’s ready for use.

feed-rdf.php

For feed-rdf.php, find the line that reads <item rdf:about="<?php the_permalink_rss(); ?>">. Place the cursor just after the > and press the Enter key three times. Press the up arrow key to go to the middle of the blank lines, then copy the snippet below and paste it onto this line:

<?php 
    if (has_post_thumbnail($post->ID)){
        echo '<enclosure url="'.get_the_post_thumbnail_url($post->ID).'">'.get_the_post_thumbnail($post->ID, $size='post-thumbnail').'</enclosure>';
    } 
?>

Save the file and it’s ready for use.

feed-rss.php

The feed-rss.php file conforms to 0.92 RSS specification, which is one of the earliest syndication standards. Since it was an early standard, it was much simpler and didn’t have much complexity to it. This is reflected in the feed-rss.php file, which is much smaller and simpler than the others.

Find the line that reads <item>. Place the cursor just after the > and press the Enter key three times. Press the up arrow key to go to the middle of the blank lines, then copy the snippet below and paste it onto this line:

<?php 
    if (has_post_thumbnail($post->ID)){
        echo '<enclosure url="'.get_the_post_thumbnail_url($post->ID).'">'.get_the_post_thumbnail($post->ID, $size='post-thumbnail').'</enclosure>';
    } 
?>

Save the file and it’s ready for use.

feed-rss2.php

The RSS2 specification is more complex, so the feed-rss2.php file is larger than the others. Despite this, the instructions are the same as they are for feed-rss.php.

Find the line that reads <item>. Place the cursor just after the > and press the Enter key three times. Press the up arrow key to go to the middle of the blank lines, then copy the snippet below and paste it onto this line:

<?php 
    if (has_post_thumbnail($post->ID)){
        echo '<enclosure url="'.get_the_post_thumbnail_url($post->ID).'">'.get_the_post_thumbnail($post->ID, $size='post-thumbnail').'</enclosure>';
    } 
?>

Save the file and it’s ready for use.

Conclusion

After the files are saved, they will need to be installed on the web server. If a person was able to download the files from the web server, they should have access to upload them. They need to be put in the wp-includes/ folder and should overwrite the original versions from WordPress. After the files are uploaded, the server may or may not need to be restarted. Restarting the server will cause the feeds to be updated automatically, using the new versions of the files.

One final word of note is that whenever WordPress is updated, these files may be overwritten during the update process. After every update, it’s a good idea to check the files and see if the modified versions have been replaced by official WordPress versions. If they’ve been replaced, the new versions may need to be edited according to the instructions above.

I hope this information is useful. Please feel free to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *