WordPress Add image media content and enclosure tags to RSS

Just a few lines to add media:content and enclosure tags to the built-in WordPress RSS feed.  No plugin required. No rebuilding the RSS template.
add_action( 'rss_item', 'custom_feed_meta', 5, 1 );
add_action( 'rss2_item', 'custom_feed_meta', 5, 1 );

function custom_feed_meta() {
	global $post;

	if(!has_post_thumbnail($post->ID)) return;
	$thumbnail_id = get_post_thumbnail_id( $post->ID );
	if(empty($thumbnail_id)) return;
	
	$image_full = wp_get_attachment_image_src( $thumbnail_id, 'full' );
	$image_medium = wp_get_attachment_image_src( $thumbnail_id, 'medium' );
	$image_thumb = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' );
	
	if ($image_full !== false) {
		echo '<enclosure url="' . $image_full[0] . '" length="' . $image_full[4] . '" type="image/jpg" />' . "\n";
		echo '<media:content url="' . $image_medium[0] . '" width="' . $image_medium[1] . '" height="' . $image_medium[2] . '" medium="image" type="image/jpg" />' . "\n";
		echo '<media:thumbnail url="'. $image_thumb[0] . '" width="' . $image_thumb[1] . '" height="' . $image_thumb[2] . '" medium="image" type="image/jpg" />' . "\n";
	}

}
Posted by
Snippet Viewed 3189 times.

Share your WordPress code snippets:

  • Get some recognition & a link back to your site.
  • Create your own code library.
  • Help your fellow developers, as they have helped you.

 

Submit | Browse

Most Recent wordpress snippets

Most Viewed