Wordpress - How to add post featured image to RSS item tag?
You could do it by adding an action to the hook 'rss2_item' like so:
add_action('rss2_item', function(){
global $post;
$output = '';
$thumbnail_ID = get_post_thumbnail_id( $post->ID );
$thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail');
$output .= '<post-thumbnail>';
$output .= '<url>'. $thumbnail[0] .'</url>';
$output .= '<width>'. $thumbnail[1] .'</width>';
$output .= '<height>'. $thumbnail[2] .'</height>';
$output .= '</post-thumbnail>';
echo $output;
});
Building off of codekipple's great answer, here's my modified implementation, which uses the valid Media RSS element media:content
element (spec) and checking for the existence of a thumbnail/featured image:
function dn_add_rss_image() {
global $post;
$output = '';
if ( has_post_thumbnail( $post->ID ) ) {
$thumbnail_ID = get_post_thumbnail_id( $post->ID );
$thumbnail = wp_get_attachment_image_src( $thumbnail_ID, 'thumbnail' );
$output .= '<media:content xmlns:media="http://search.yahoo.com/mrss/" medium="image" type="image/jpeg"';
$output .= ' url="'. $thumbnail[0] .'"';
$output .= ' width="'. $thumbnail[1] .'"';
$output .= ' height="'. $thumbnail[2] .'"';
$output .= ' />';
}
echo $output;
}
add_action( 'rss2_item', 'dn_add_rss_image' );
Note: Include the xmlns attribute here to make it validate. WordPress's initial install doesn't include that namespace declaration, and while you can change it, so can other themes/plugins.
More details on the other attributes etc. are in my non-WordPress-specific answer here.
This integrates with MailChimp's RSS newsletter building.