How to grab first image attached to post and display in RSS feed?

i actually just finished working on a site that needed images in his feeds so i ended up using this:

function ba_post_image_feeds($content) {
    global $post,$posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];

    if(!empty($first_img)){   

        $content="<div>" . $first_img . '</div>' . $content;
    }
    return $content;
}


add_filter('the_excerpt_rss', 'ba_post_image_feeds');
add_filter('the_content_feed', 'ba_post_image_feeds');

Leave a Comment