How to add post featured image to RSS item tag?

I am able to add a post featured image to the RSS feed like so:

function insertThumbnailRSS($content) {
    global $post;
    if(has_post_thumbnail($post->ID)){
        $content="".get_the_post_thumbnail($post->ID, 'thumbnail', array('alt' => get_the_title(), 'title' => get_the_title(), 'style' => 'float:right;')).''.$content;
    }
    return $content;
}
add_filter('the_excerpt_rss', 'insertThumbnailRSS');
add_filter('the_content_feed', 'insertThumbnailRSS');

However, upon examining the XML generated for the RSS feed, I noticed it sticks the featured image into the XML description item tag.

How can I insert post featured image into it’s own RSS feed item tag of let’s say “image”, rather than just inserting it in with the post’s content?

3 s
3

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;
});

Leave a Comment