If you use Mailchimp or a similar mailing list service, you’ll probably find this code. This code ensures that the featured image of each post is included in the RSS feed as a element, enhancing the feed with images.
Like all hooks this code needs to be inserted into fucntions.php of your child theme. Or use the Code Snippets plugin (or similar). Good Luck:)
/**
 * Add post thumbnail as <media:content> in RSS feed.
 */
function dn_add_rss_image() {
    global $post;
    $output = '';
    
    // Check if the post has a featured image
    if ( has_post_thumbnail( $post->ID ) ) {
        $thumbnail_ID = get_post_thumbnail_id( $post->ID ); // Get the ID of the post thumbnail
        $thumbnail = wp_get_attachment_image_src( $thumbnail_ID, 'large' ); // Get the image source, width, and height
        // Construct the <media:content> element for RSS feed
        $output .= '<media:content xmlns:media="http://search.yahoo.com/mrss/" medium="image" type="image/jpeg"';
        $output .= ' url="' . esc_url( $thumbnail[0] ) . '"';
        $output .= ' width="' . esc_attr( $thumbnail[1] ) . '"';
        $output .= ' height="' . esc_attr( $thumbnail[2] ) . '"';
        $output .= ' ></media:content>';
    }
    
    // Output the constructed <media:content> element
    echo $output;
}
// Hook the function to the 'rss2_item' action to add the thumbnail to RSS feed items
add_action( 'rss2_item', 'dn_add_rss_image' );


Leave a Reply