Featured Images as media:content in RSS
Adds each post's featured image to the RSS feed as a media:content element, the format email newsletter tools and feed readers use to show a thumbnail.
Opens WPIDE on
Code
// Image size used in the feed.
$wpide_feed_image_size = 'large';
add_action( 'rss2_ns', function () {
echo 'xmlns:media="http://search.yahoo.com/mrss/"' . "\n";
} );
add_action( 'rss2_item', function () use ( $wpide_feed_image_size ) {
$post = get_post();
if ( ! $post || ! has_post_thumbnail( $post ) ) {
return;
}
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post ), $wpide_feed_image_size );
if ( ! $image ) {
return;
}
$mime = get_post_mime_type( get_post_thumbnail_id( $post ) );
printf(
"<media:content url=\"%s\" medium=\"image\" width=\"%d\" height=\"%d\"%s />\n",
esc_url( $image[0] ),
(int) $image[1],
(int) $image[2],
$mime ? ' type="' . esc_attr( $mime ) . '"' : ''
);
} );
Notes
Mailchimp, Feedly and most RSS-to-email services read this element. The image size is set at the top.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.