8

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?

10

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;
});
  • 1
    I found this after hours researching... great solution, thanks! – cawecoy May 6 '16 at 23:00
7

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.

  • 2
    Good answer. I've tried WordPress plugins that add the image to RSS but actually they only add it as part of the content. This adds the image as it's own entity so you have more control over layout when using MailChimp's merge tags. – Ian Lunn Jan 23 '18 at 11:34
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.