pixel

How To: Add Featured Images to WordPress RSS Feed Using Enclosure

Gary Green
Gary Green
June 26, 2021

WordPress has native RSS feed functionality, making setup extremely easy, but you may have noticed including featured images or customising can be tricky.

Feed aggregators and tools such as MailChimp and Zapier use your website’s RSS feed to fetch data. If you’re looking to use your images with these services, you’ll need to know how to display featured images in the WordPress RSS feed.

While there are plugins that can achieve this, we find they often conflict with a range of other plugins or otherwise deliver a hit to site performance due to the additional features they also entail. So instead, we advocate the use of a simple PHP code via your theme’s functions.php file or the highly recommended Code Snippets plugin if you use a theme-less builder such as Oxygen.

This brief article explains how to display featured images within your WordPress feed with an enclosure tag (without using a plugin).

Our Use Case

We believe in the efficiency and power of automation. Our content clients regularly share their latest blog articles, published on their website, to social media networks, such as Facebook, Twitter, and LinkedIn. We rely on Zapier to automatically share these articles on our clients’ social media pages.

In the last couple of weeks, we noticed an update to LinkedIn’s API, which no longer automatically fetched the article’s image sent through via Zapier, no matter what tags were present on the page. This wasn’t the case before, where although the image field within the ‘Zap’ was left blank, the image fed through just fine.

This change resulted in articles shared with just a link, without the fancy image cardholder that usually appears. To rectify this, we needed to hook into WordPress’s RSS functionality and pass the featured image of the articles in a way that can read by Zapier. 

We tried a couple of plugins here, but although they did well to find and send the images to the RSS feed, they were included within the content, were simply unreadable by Zapier or didn't allow you to choose which images to use and their corresponding size. This is where the <enclosure> element comes in.

What Is Enclosure?

The <enclosure> element allows a way to attach multimedia content to be included with an associated entry within an RSS feed.

The <enclosure> element can be read by Zapier, which means you can assign it to the Zap’s image field, and it is correctly fed through to LinkedIn or any other social media platform.

Code Snippet to Add Enclosure Image to RSS Feed

Firstly, we need to hook into WordPress’ RSS functionality, which can be accessed via the rss2_item action hook. This is triggered immediately after the default output of an rss2 feed. When this hook occurs, we want to run our function add_post_featured_image_enclosure.

add_action( 'rss2_item', 'add_post_featured_image_enclosure' );

Then, we define our function, as below:

function add_post_featured_image_enclosure() {
	if ( ! has_post_thumbnail() )
		return;

	$thumbnail_size = apply_filters( 'rss_enclosure_image_size', 'large' );
	$thumbnail_id = get_post_thumbnail_id( get_the_ID() );
	$thumbnail = image_get_intermediate_size( $thumbnail_id, $thumbnail_size );

	if ( empty( $thumbnail ) )
		return;

If you would like to fetch a different image size, you simply need to change the $thumbnail_size property from 'large' to your desired image size, typically 'thumbnail', 'medium' and 'full'.

Then, we need to simply print the enclosure element using the correct syntax and paths, as defined below. 

	$upload_dir = wp_upload_dir();

	printf( 
		'<enclosure url="%s" length="%s" type="%s" />',
		$thumbnail['url'], 
		filesize( path_join( $upload_dir['basedir'], $thumbnail['path'] ) ), 
		get_post_mime_type( $thumbnail_id ) 
	);

The Final Snippet

The overall code snippet to add to your functions.php file is:

add_action( 'rss2_item', 'add_post_featured_image_as_rss_item_enclosure' );

function add_post_featured_image_as_rss_item_enclosure() {
	if ( ! has_post_thumbnail() )
		return;

	$thumbnail_size = apply_filters( 'rss_enclosure_image_size', 'large' );
	$thumbnail_id = get_post_thumbnail_id( get_the_ID() );
	$thumbnail = image_get_intermediate_size( $thumbnail_id, $thumbnail_size );

	if ( empty( $thumbnail ) )
		return;

	$upload_dir = wp_upload_dir();

	printf( 
		'<enclosure url="%s" length="%s" type="%s" />',
		$thumbnail['url'], 
		filesize( path_join( $upload_dir['basedir'], $thumbnail['path'] ) ), 
		get_post_mime_type( $thumbnail_id ) 
	);
}

With the addition of this, we were able to fetch the featured images correctly through Zapier, restoring our automation process with clients’ blog articles whilst also ensuring no further plugins were required.

If you’d like any assistance with your website, we at Key Business Marketing can help. Get in touch with us via the contact form below or call us on 02037 282 848.

Tell us about your project...
Fill in your details to get started and join over 50 businesses enjoying Key Business Marketing's expertise..
Contact Form Demo (#1)
We adhere to strict GDPR rules and do not reveal or sell your data to any third-parties. For more, please read our Privacy Policy.
Latest Insights
February 13, 2024
A Guide To Using ChatGPT To Carry Out Keyword Research

Discover how to harness the potential of ChatGPT for advanced keyword research in SEO with our comprehensive guide.

January 12, 2024
Bolstered Advertising From Google On YouTube Shorts

Explore the latest Google advancements in YouTube Shorts advertising! Discover how Google is boosting brand visibility and addressing revenue challenges.

December 15, 2023
How Do I Manage My WordPress Website’s Email Services?

Learn how to effectively manage your WordPress website's email services for optimal performance. Explore the three methods of sending emails in WordPress

November 26, 2023
Monitoring Your Website Performance And Optimizing Accordingly

Explore the art of monitoring and optimising your website's performance. Discover dedicated tools to prevent downtime, enhance user experience, and meet Google's performance standards.

November 10, 2023
Project Magi: A Major Change To Google Search

Discover the revolutionary shift in Google's search landscape with Project Magi! This upcoming AI-powered search experience makes some big promises!

October 23, 2023
Top Tips For WordPress Security

Discover essential tips to fortify your WordPress website's security. Safeguard your site with expert advice in this comprehensive guide

September 29, 2023
A Guide To WordPress Maintenance Plans

Mastering WordPress Maintenance Plans: Organize your website tasks efficiently, from backups to plugin updates.

September 18, 2023
Solutions For The Most Frequently Seen WordPress Errors

Discover expert solutions for the most common WordPress errors and streamline your website management.

September 2, 2023
Top Tips For Managing Your WordPress Website

Master the art of WordPress website management with our expert tips! Learn how to streamline maintenance, boost performance, and enhance security

View Our latest insights »
Get the latest digital marketing insights delivered straight to your inbox
Newsletter Form (#2)
We care about the protection of your data. No spam. Unsubscribe anytime.
02037 282 848
Mon-Fri 9am - 5.30pm
contact@keybusinessmarketing.co.uk
Digital marketing guidance at your fingertips
Copyright © 2022 KEY BM LTD Reg: E&W 11055943
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram