Get featured image from post on RSS feed

I want to add the featured image to my RSS feed.

function rss_post_thumbnail($content) {

    global $post;

    if(has_post_thumbnail($post-ID)) {
        $content = 'p' . get_the_post_thumbnail($post-ID) . '/p' . get_the_content();
    }
    return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail', 20, 1);
add_filter('the_content_feed', 'rss_post_thumbnail', 20, 1);

With this snippet the thumbnail is shown, but the content is not generated correct. It is shown with shortcodes and its not formatted...

When i use the snippet below, where i remove the ordering of filters, the content is shown, but the featured image is missing:

function rss_post_thumbnail($content) {

    global $post;

    if(has_post_thumbnail($post-ID)) {
        $content = 'p' . get_the_post_thumbnail($post-ID) . '/p' . get_the_content();
    }
    return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

Topic the-content post-thumbnails rss Wordpress

Category Web


I just found the right solution:

function rss_post_thumbnail($content) {

    global $post;

    if(has_post_thumbnail($post->ID)) {
        $output = '<p>' . get_the_post_thumbnail($post->ID) . '</p>';
    }
    return $output . $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail', 20, 1);
add_filter('the_content_feed', 'rss_post_thumbnail', 20, 1);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.