In this quick tip, we’ll learn about tweaking our blog posts feed in order for MailChimp RSS-to-Email service to fetch it and actually parse the posts featured images and a read more button that links to the actual post.

If you don’t have any RSS-to-email campaign yet, you can learn more about sharing your blog posts with MailChimp via RSS feeds.

We will tweak WordPress RSS feed to display some HTML code which consists of the post featured image, post excerpt and a read more button. Now we want this to occur only for MailChimp and we don’t want these elements to show for our feed readers, so we’ll add a query parameter to our MailChimp Feed URL so we can base our code on it.

Edit RSS feed URL setting of your campaign and append ‘?mailchimp_feed_rss=1’ to it. For example if it was ‘http://example.com/blog/feed/’, then it should become ‘http://example.com/blog/feed/?mailchimp_feed_rss=1’.

The above step is required, otherwise the feed filters will not be applied.

You can as well ignore posts from certain categories not to be parsed within the query of our MailChimp feeds, you can pass the categories identifiers to the `feed_ignore_categories` global in the plugin code.

Here’s the full code in a plugin:

<?php
/*
Plugin Name: Tweak MailChimp Feeds RSS
Plugin URI: https://samelh.com/blog
Description: Tweak MailChimp RSS Feeds to add the featured image, excerpt and a read more button.
Author: Samuel Elh
Version: 0.1
Author URI: https://go.samelh.com/buy-me-a-coffee
*/

defined ( 'ABSPATH' ) || exit ( 'Direct access not allowed.' . PHP_EOL );

$GLOBALS['feed_ignore_categories'] = array( 
    /**
      * To ignore posts from certain categories,
      * enter your category IDs here separated by commas. example
      * 2, 19, 28
      */
);

function filter_the_content_feed( $content ) {
    global $post;

    $featured_image = has_post_thumbnail($post) ? get_the_post_thumbnail($post) : null;
    
    $excerpt = trim( wpautop(get_the_excerpt( $post->ID )) );

    $read_more = '<table border="0" cellpadding="0" cellspacing="0" style="margin: 0 auto;background-color:#25aae2; border-radius:5px;">';
    $read_more .= '<tr>';
    $read_more .= '<td align="center" valign="middle" style="color:#FFFFFF; font-family:Helvetica, Arial, sans-serif; font-size:16px; font-weight:bold; letter-spacing:-.5px; line-height:150%;">';
    $read_more .= '<a href="' . get_the_permalink($post) . '" target="_blank" style="padding: 15px 30px; line-height: 40px;color:#FFFFFF; text-decoration:none;">Read More</a>';
    $read_more .= '</td>';
    $read_more .= '</tr>';
    $read_more .= '</table>';

    return $featured_image . $excerpt . $read_more;
}


function pre_get_posts_mailchimp_rss($query) {
    $query->query_vars['category__not_in'] = $GLOBALS['feed_ignore_categories'];
}

if ( isset($_REQUEST['mailchimp_feed_rss']) ) {
    remove_all_filters('the_content');
    add_filter('wpseo_include_rss_footer', '__return_false');
    add_action('pre_get_posts', 'pre_get_posts_mailchimp_rss');
    add_filter( 'the_excerpt_rss', 'filter_the_content_feed', 999 );
}

You can download this plugin from Github.

Digital Ocean

Cheap Cloud SSD Hosting

Get a VPS now starting at $5/m, fast and perfect for WordPress and PHP applications

Sign Up with $10 Credit