How to Add Featured Images to Your WordPress RSS Feed

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.

You might also like

Real-world alchemy

Question: what’s a simple way to pandemic proof your business? Or what about the next recession? There’s always one around the corner for one reason

Read More »

$5,857.20

That’s been the top, so far. The highest I’ve hit on Skillshare. And that IS just Skillshare. Doesn’t count Udemy, sales on my own website,

Read More »
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 secrets to high-paying freelance clients

What makes clients willing to pay $5,000, $10,000 even $20,000 and up for your services? Download and install my mobile app and I’ll show you. It’s free. Just click the button below:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

WHAT OTHERS ARE SAYING

Lori Grant

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

Xan Barksdale

Xan Barksdale

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

Jim DeJonge

Jim DeJonge

John has a relaxed and engaging manner. His advice is solid and the explanations are well thought out.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Steve Dimmick

Steve Dimmick

John has provided expert knowledge and advice on multiple occasions that have helped me better serve my clients. John is a Rockstar!

Daniel Mohlendick

On the Freelancing on Upwork course: “This is by far the best course i have watched on Skillshare!! Thank you so much.”

Aaron Gott

Aaron Gott

John has a particular knack for the development and training of others.

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.

Michael Skye

Michael Skye

John is a man of integrity, who gives generously of himself to projects and people he cares about.

Jason Rumley

Jason Rumley

John has a habit of over-delivering on the expectations he sets up. That’s why he’s the best.

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.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Andrew Malone

Andrew Malone

John Morris is exceptional in his ability to give focused insight into Freelancing and starting one’s business. His direct methods inspire confidence in his honesty.