DevelopmentWeb Development and RedesignWordPress

How to Show WordPress Featured Images in MailChimp RSS – Without a Plugin

We’ve already gone through how to get featured images/blog post images to show in MailChimp with the Featured Image in RSS plugin, but perhaps you don’t want to use a plugin. Maybe you’re looking for a way to edit your RSS feed without putting yet another plugin into your WordPress install. Never fear! We can help guide you through this plugin-less route.

It’s actually quite simple. What we are going to do is create a custom page template that acts as our modified RSS feed. This is also a great way you can modify your RSS feed to serve only certain content, such as if you have more than one feed going at the same time.

Step 1: Create a new page template

This will reside inside of your WordPress theme’s folder. Much like header.php or footer.php, it will act similarly. If you’ve already made page templates before, you’ll know all of the required information, but we’re going to outline it for all of our first timers.

I am going to name the filename of my page template mailchimp.php. It doesn’t really matter what you name it, but you should make sure to use something that you won’t use in the future or won’t conflict with WordPress. That means probably refrain from using page.php, index.php, archives.php, etc. If you want to be ultra sure, you can also name the file name “rss-mailchimp.php” or otherwise.

Step 2: Add the required information at the top of the page document

Basically this tells WordPress that this file is a template, so it will be selectable on the right sidebar of your pages when you go to make it. Add this at the top:

<?php 
/* 
Template Name: MailChimp
*/

Again, you can change the “template name” to anything you want, but I am going to name mine simply “MailChimp”

Step 3: Define your RSS items in your page template

You can copy this code to paste into your file after the code from step 2 and it will “just work”, but I will explain some of the items worth looking at to make sure that you have it just the way you want to.

$numposts = 10; // number of posts in feed
$posts = query_posts('showposts='.$numposts.'&cat=-1'); // replace the number 1 with the ID of your tumblelog category
$more = 1;

header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>

<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:media="http://search.yahoo.com/mrss/"
	<?php do_action('rss2_ns'); ?>
>
<channel>
	<title><?php bloginfo_rss('name'); ?> - <?php wp_title_rss(); ?> - MailChimp Feed</title>
	<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
	<link><?php bloginfo_rss('url') ?></link>
	<description><?php bloginfo_rss("description") ?></description>
	<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
	<?php the_generator( 'rss2' ); ?>
	<language><?php echo get_option('rss_language'); ?></language>
	<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
	<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
	<?php do_action('rss2_head'); ?>
	<?php while( have_posts()) : the_post(); ?>

	<item>
		<title><?php the_title_rss(); ?></title>
		<link><?php the_permalink_rss(); ?></link>
		<comments><?php comments_link(); ?></comments>
		<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
		<dc:creator><?php the_author(); ?></dc:creator>
<?php the_category_rss(); ?>

<?php if (has_post_thumbnail( $post->ID ) ): ?>
  <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
<?php endif; ?>
	<media:content url="<?php echo $image[0]; ?>" width="800" medium="image" />
		<guid isPermaLink="false"><?php the_guid(); ?></guid>
<?php if (get_option('rss_use_excerpt')) : ?>

		<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<?php else : ?>

		<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
	<?php if ( strlen( $post->post_content ) > 0 ) : ?>

		<content:encoded><![CDATA[<?php the_content() ?>]]></content:encoded>
	<?php else : ?>

		<content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
	<?php endif; ?>
<?php endif; ?>

		<wfw:commentRss><?php echo get_post_comments_feed_link(); ?></wfw:commentRss>
		<slash:comments><?php echo get_comments_number(); ?></slash:comments>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>

	</item>
	<?php endwhile; ?>

</channel>
</rss>

Explanation of items of interest: $numposts is the number of posts in this feed. You can change it depending on what you’re doing with your feed. $posts, if you change the number on &cat=-1, can make it so your feed only serves posts made to a certain category. For example, if my category “advertising” is category ID 3, and I only wanted to send emails in the advertising category, I would change it to &cat=3

Important thing you need that is not in the standard RSS feed: You’ll notice under RSS version type there is an additional line: xmlns:media=”http://search.yahoo.com/mrss/” MailChimp uses media content blocks to read images, so you will need to first define that you are using this sort of markup with this line. I have then added after the category block 

<?php if (has_post_thumbnail( $post->ID ) ): ?>
  <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
<?php endif; ?>
<media:content url="<?php echo $image[0]; ?>" width="800" medium="image" />

Which basically says “if there is a featured image, get the URL for the full size of this featured image”, and then after that says “here is image media, here is the URL of it”, which is what MailChimp will then use to pull the image. You may want to define  a width parameter here if you are using the ‘full’ size featured images like I am, so you don’t end up making anyone’s inbox explode! Remember to always preview your campaigns before hitting send.

Step 4A: Make a page that uses this page template

Go into WordPress and click pages, then click add new. You will want to use the page attributes selector to pick your new “mailchimp” (or other name) template. Give your page a desired page slug (I am using /mailchimp, but you might want to use something else), and hit publish. You may also want to noindex this page as well.

Page Attributes in WordPress

If you don’t see “page attributes”, scroll to the top of the page and click “screen options”, then make sure “page attributes” is checked off. It should show now.

Screen Options

Step 4B: A Different method of adding your template…

Don’t want your template to take up space on your front end somewhere? Never fear! Let’s append it to /feed and keep all our RSS items in one spot. Add this code to functions.php:

add_action('init', 'customRSS');
function customRSS(){
        add_feed('mailchimp', 'customRSSFunc');
}

function customRSSFunc(){
        get_template_part('rss', 'mailchimp');
}

In the above code, it will be looking for a file titled “rss-mailchimp.php”, but if you’ve just named it “mailchimp.php”, that’s OK too, just remove ‘rss’ from the array like so:

get_template_part(‘mailchimp’);

Before you can use your new url, you’ll want to flush your rewrite rules… Head on over to Settings -> Permalinks in WordPress and just hit save without changing anything. That should do it!

Step 5: Load your new URL into MailChimp

Instead of using /feed or /rss in your campaign setup, you’ll now want to use the URL you defined in step 4A. Mine will be http://www.website.com/mailchimp

For step 4B, your url will be http://www.website.com/feed/mailchimp

Step 6: Use the *|RSSITEM:IMAGE|* tag

This doesn’t work out of the box with WordPress and MailChimp, but it should be working now! Just place that merge tag where you want your image to appear, and when you test it, you should now see your featured images showing. Hooray!

Have questions or comments? Let us know below!

Want to get updates like this delivered straight to your inbox?
Subscribe to our blog to receive website design tips, development tricks, the latest in SEO, and more. We don't share your email address with anyone and you can unsubscribe at any time.
* indicates required

Leave a Reply

Your email address will not be published. Required fields are marked *