X

Adding Feature Images to the WordPress RSS Feed

The WordPress RSS feed is great. We use it with third-party services, such as MailChimp. One key datum that isn’t included in the feeds by default is the featured image for posts (and pages). This article will address that with a simple ‘action‘ hook that adds the featured image to the item’s feed.

/**
 * Filter: Add feature image to RSS feeds
 *
 * @param $content string   the content
 * @return string           the potentially modified content
 */function rss_featured_image($content) {
    global $post;

    // if the isn't an associated featured image, then return just return the content
    if (!has_post_thumbnail()) return $content;

    // featured image layout
    $styling = ['style' => 'float: none; margin: auto;'];

    // add a filter to return null for srcset
    add_filter( 'wp_calculate_image_srcset_meta', '__return_null' );
    $featured_image = get_the_post_thumbnail($post->ID, 'medium', $styling);

    // remove the above filter
    remove_filter( 'wp_calculate_image_srcset_meta', '__return_null' );

    // return the featured image and the $content
    return $featured_image . $content;
}

add_filter('the_excerpt_rss', __NAMESPACE__.'\\rss_featured_image');
add_filter('the_content_feed', __NAMESPACE__.'\\rss_featured_image');

Using a plugin or WP developer

Don’t fancy getting you hands dirty with code? Search the WP plugins repo, or get in touch for further help.

Related Post

This website uses cookies.