How to Add Featured Images to Your WordPress RSS Feed

DISCLAIMER: This post may contain “affiliate” links to products and services I recommend. I’ll receive a small commission if you decide to purchase one of these products or services. I only recommend products I genuinely believe will help you in running your freelance business.

If you want to add featured images to your WordPress RSS feed (as enclosures), here’s what to do. First, you need to hook into the RSS feed as each item is built. This hook will do that:

<?php
add_action( 'rss2_item', 'rss_add_featured_image' );

Next, in our callback function, we need to check if the current post has a featured image. We’ll use has_post_thumbnail() for that:

<?php
if ( function_exists( 'has_post_thumbnail' ) && has_post_thumbnail( $post->ID ) ) {
    // do stuff here   
}

No, we need to get the URL of the featured image if it does exist. Here, we’ll need to run through a couple steps like this:

<?php
$attachment_id = get_post_thumbnail_id($post->ID);
$featured_image = wp_get_attachment_image_src( $attachment_id, 'post-thumbnail' );
$url = $featured_image[0];

Enclosures require you set not only the URL, but also the length of the file (in bytes) and the MIME type. Here’s how to do that:

<?php
$length = filesize(get_attached_file($attachment_id));
$type = get_post_mime_type($attachment_id);

And, finally, we can out put the enclosure using printf():

<?php
printf('<enclosure url="%s" length="%s" type="%s" />', $url, $length, $type);

This can all be inside a plugin or in your theme’s functions.php file. Although, I’d recommend using a plugin so if you change themes, you don’t have to transfers this code over to your new theme. And, if you’re ready to get serious about building WordPress plugins, then enroll in my WordPress plugin course. Inside, you build a contact form plugin, but, more importantly, you’ll learn fundamental WordPress plugin-building skills like creating custom post types, adding shortcodes, adding custom columns to the post table and more. You can learn more here.

Do you want high-paying freelance clients?

I’ll show you what I learned over the last 15 years about building a 100K freelance business from complete scratch. What I did to land projects with Inc. Magazine, Tim Ferris, Lewis Howes, Michael Hyatt and others. Just enter your email address in the box below and let’s get started:

Leave a Reply on Twitter

Due to excessive spam, I’ve disabled comments on my website. If you have a question or would like to leave a comment, don’t hesitate to contact me on Twitter.

You might also like

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

The simple secret to high-paying freelance clients?

Enter your email below and I’ll show how to charge more and build a 100K freelance business from scratch:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

WHAT OTHERS ARE SAYING

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.

Sukh Plaha

John is a fantastic and patient tutor, who is not just able to share knowledge and communicate it very effectively – but able to support one in applying it. However, I believe that John has a very rare ability to go further than just imparting knowledge and showing one how to apply it. He is able to innately provoke one’s curiosity when explaining and demonstrating concepts, to the extent that one can explore and unravel their own learning journey. Thanks very much John!

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Chris Aitken

He significantly improved my site through his expert knowledge of PHP, CSS and Javascript. Would definitely recommend John to others.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.