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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

Building off codekipple and D_N, I wanted a few more attributes in my media:content so here’s what I did:

function add_media_content_to_feed() {
  global $post;

  $post_id = $post->ID;

  if(!has_post_thumbnail($post)) {
    return;
  }

  $thumbnail_size = 'large';
  $thumbnail_id   = get_post_thumbnail_id($post_id);

  $file           = image_get_intermediate_size(get_post_thumbnail_id(), $thumbnail_size);

  $url            = $file['url'];
  $type           = $file['mime-type'];
  $height         = $file['height'];
  $width          = $file['width'];
  $file_size      = '';

  $path           = $file['path'];
  if($path && 0 !== strpos($path, '/') && !preg_match('|^.:\|', $path) && (($uploads = wp_get_upload_dir()) && false === $uploads['error'])) {
    $path         = $uploads['basedir']."/$path";
    $file_size    = filesize($path);
  }

  echo sprintf(__('<media:content url="%s" type="%s" medium="image" height="%s" width="%s" fileSize="%s" />'),
    $url,
    $type,
    $height,
    $width,
    $file_size
  );

}
add_action('rss2_item', 'add_media_content_to_feed');

codekipple’s answer also actually adds the image below all feed content. I wanted my image above content, so I did this:

function add_featured_image_to_feed($content, $feed_type) {
  global $post;
  $post_id = $post->ID;
  if(has_post_thumbnail($post)) {
    $content = '<div class="feed-image">'.get_the_post_thumbnail($post_id, 'large').'</div>'.$content;
  }
  return $content;
}
add_filter('the_content_feed', 'add_featured_image_to_feed', 10, 9999);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x