bluelinecombo
8/1/2017 - 8:49 PM

Add featured image to Wordpress RSS feed

Add featured image to Wordpress RSS feed

// =============================================================================
// RSS feed
// =============================================================================

// Look for and add featured image into the beginning of the RSS <description> and <content:encoded> so Hubspot will pick it up
$featuredToRss = function($content) {
    global $post;

    if (has_post_thumbnail($post->ID)) {
        // Get thumbnail src
        $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), large);

        if (!isset($thumbnail[0])) {
            return $content;
        } else {
            $thumb_src = $thumbnail[0];
        }

        // Adding width as an inline style can be beneficial even though Hubspot now has this feature
        $content = '<div class="featured-image-wrapper"><img src="' . $thumb_src . '" style="width: 600px; height: auto; margin-bottom: 10px;"></div>' . $content;

    }

    return $content;
};

add_filter('the_excerpt_rss', $featuredToRss);
add_filter('the_content_feed', $featuredToRss);