Adding the Mailchimp Required < media: content > Tag to your WordPress RSS Feed Output

For creating RSS driven campaigns in Mailchimp using the *|FEEDIMAGE|* merge tag

You’ll need to copy and paste the following code snippet to your WordPress theme’s function.php file.

The code, when executed, will check for the existence of a thumbnail/featured image in your current feed output and apply the required media:content tag. This allows Mailchimp to identify and pull in your post imagery when populating RSS driven campaigns.

function dn_add_rss_image() {
    global $post;

    $output = '';
    if ( has_post_thumbnail( $post->ID ) ) {
        $thumbnail_ID = get_post_thumbnail_id( $post->ID );
        $thumbnail = wp_get_attachment_image_src( $thumbnail_ID, 'thumbnail' );

        $output .= '<media:content xmlns:media="" medium="image" type="image/jpeg"';
        $output .= ' url="'. $thumbnail[0] .'"';
        $output .= ' width="'. $thumbnail[1] .'"';
        $output .= ' height="'. $thumbnail[2] .'"';
        $output .= ' ></media:content>';
    echo $output;
add_action( 'rss2_item', 'dn_add_rss_image' );