Tag Archives: rss enclosure

How to add posts’ featured images to WordPress RSS feeds

The standard WordPress RSS-feeds don’t include posts featured image. Below code adds the medium-format thumbnail to each item in a RSS2 standards-compliant manner by inserting it as an enclosure.

add_action('rss2_item', 'add_enclosure_thumb'); function add_enclosure_thumb() { global $post; if(has_post_thumbnail($post->ID)) { $thumbUrl = get_the_post_thumbnail_url($post->ID,"medium"); if ((substr($thumbUrl, -4) === "jpeg") || (substr($thumbUrl, -3) === "jpg")) { $mimeType="image/jpeg"; } else if (substr($thumbUrl, -3) === "png") { $mimeType="image/png"; } else if (substr($thumbUrl, -3) === "gif") { $mimeType="image/gif"; } else { $mimeType="image/unkown"; } $thumbSize = filesize(WP_CONTENT_DIR.str_replace(WP_CONTENT_URL,'',$thumbUrl)); echo "\n"; } }

A more advanced & flexible approach would be to add support for the media RSS namespace, but the above suffices for the purpose I have in mind.