Custom MailChimp auto-feed from RSS feed

Custom MailChimp auto-feed from RSS feed






Everyone knows how to do a newsletter. Many people know how to create trigger chains. Someone knows how to set up a feed from the RSS feed of a blog. We had to dig deeper, because we wanted our newsletter to be not simple, but beautiful and detailed. After all, it is made from an interesting blog, on which we also spent a lot of effort.







For each detail in a simple-looking letter I had to fight with this cute monkey. Everything worked out. We tell you how.







Mailhimp is an English-language and sometimes illogical service in the interface. But he knows how much and is so popular that meticulous instructions were written for him on delivering content to readers in all possible ways.







We have also chosen Mailhimp for our PromoPult blog postings. One of the features of Mailhimp is auto campaigns. You can schedule regular mailings, individual letters, set up welcome letters and much more.







This story is about setting up auto-posting of blog posts. A digest of fresh materials to the subscriber's mail once a week.







The content of the article
The standard option for the auto-link and why it did not suit us

How we solved the problem

Create RSS feeds

What's inside the tape

Making a feint with your ears

Output content from the feed in the email template

On a separate line:

TOTAL: If everything is standard and easy for you, use standard MailChimp tools and do not worry

Links to the topic of RSS feeds in MailChimp:



The standard option for the auto-link and why it did not suit us



Mailhimp itself has excellent documentation on any functions, and specifically about this too: Share Your Blog Posts with Mailchimp . Translations into Russian can also be found, of varying degrees of adequacy.







Briefly, what you need to make an auto-link:







  1. Create a separate RSS feed for MailChimp (or even not create, but use a standard feed).
  2. Create a campaign, specify the source, configure the sending conditions.
  3. Create and select a template.
  4. Done. You are gorgeous!


What did not suit us is that it is simply impossible to add any of our own things.







There are basic data from the RSS feed that Mailhimp understands:









And we have additional things in a separate post:









And you can’t put this into a regular RSS feed - there are no corresponding tags. Custom MailChimp tags do not understand and should not be blamed for it. We need to look for a solution that will suit everyone and not break.







The data that needs to be shown in a separate post card in the digest template is tied to the appearance. The designer drew, the typesetter suffered a lot and made up the letter.







Custom MailChimp auto-feed from RSS feed






PromoPult blog post card in the weekly digest









How we solved the problem



Create RSS feeds



The first thing to do is create a separate RSS feed for MailChimp. There are already several similar feeds on the PromoPult blog: for Yandex.Zen and for Turbo pages .







You can create a new feed through add_feed () . Here is the code in the theme function file:







functions.php:









 /* start       MailChimp */ add_action( 'init', 'customRSSforMC' ); function customRSSforMC() { add_feed( 'mchimp', 'customRSSforMCFunc' ); } function customRSSforMCFunc() { get_template_part( 'rss', 'mchimp' ); } /* end       MailChimp */
      
      





In line No. 7 get_template_part ( 'rss', 'mchimp' );



- indicates that the template for the feed lies in the root of the theme folder and is called rss-zen.php



.







After creating the file and adding the function, you need to go to the WordPress admin panel, the CNC settings: “Console → Settings → Permalinks” and click the “Save Changes” button so that WordPress updates the /feed/URL



settings and opens at the given /feed/URL



in the first parameter of line No. 4 new RSS feed.







You need to do this once when creating the tape.







See our example: https://blog.promopult.ru/feed/mchimp







What's inside the tape



Inside the feed itself is a regular WordPress post loop: query_posts () . This allows you to select and filter posts very flexibly: as if you were setting up individual categories, pages with selections, archives, and everything you need for WordPress content collections.







But before you come to the perfect solution to the problem, there were several attempts to cram the data important for writing inside the standard designs of RSS feeds and use them in MailChimp.







For example, there was such an option for the comment counter:







 <item> [...] <title><?php the_title_rss(); ?></title> <link><?php the_permalink_rss(); ?>?utm_source=newsletter</link> //      1,    <?php if (get_comments_number() >= 1) { ?> <commentsCounter><?php comments_number('0', '1', '%'); ?></commentsCounter> <?php } ?> [...] </item>
      
      





This option, of course, works in terms of data: everything that is needed is obtained and correctly displayed. But MailChimp does not understand the <commentsCounter>



, like any other non-standard tags.







Also, for example, with dates. MailChimp has its own tag: *|RSSITEM:DATE:d/m/y|*



, and you can pass date formatting options to it . But, unfortunately, in the letter it does not look human at all. About the date translation in general, you can not talk.







Another problem is the pictures. You have suffered and made up your beautiful writing. You need to display a picture. In the email template, you do this:







 <img src="*|RSSITEM:IMAGE|*" alt="*|RSSITEM:TITLE|*" />
      
      





But the MailChimp parser can come and break the layout by adding its properties, classes and other code to the <img ... />



image tag.







Tip. If you use standard feed and tags, be sure to enable the checkbox “ Resize images of the RSS-feed in accordance with the template ” in the campaign settings:







Custom MailChimp auto-feed from RSS feed






Intermediate conclusions: something goes wrong, standard options are not enough, the letter is terrible and not humane. Non-standard data cannot be displayed.







Making a feint with our ears: marking everything ourselves



If nothing standard can be done in the RSS feed, so that the MailChimp service parser understands this correctly, then you can transfer the finished marked-up and folded piece of code for the post card simply in the <description>



separate <item>



element in the feed.

There is only a separate minus: all styles for writing should be inline, that is, everything that is described through .class



turns into styles in the attribute style=”...”



.







 <!--   : --> <style> .post-meta { Margin: 8px 0; } .post-category { border-radius: 3px; border: #adb2b2 1px solid; color: #adb2b2 !important; border-bottom: #adb2b2 1px solid !important; padding: 2px 6px; font-size: 13px; line-height: 13px; Margin: 0 8px 0 0; } </style> <div class="post-meta"> <a href="https://blog.promopult.ru/category/seo" target="_blank" class="post-category">SEO:    </a> </div> <!--   : --> <div style="Margin: 8px 0;"> <a href="https://blog.promopult.ru/category/seo?utm_source=newsletter" target="_blank" style="Margin: 0 8px 0 0; border: #adb2b2 1px solid; border-bottom: #adb2b2 1px solid !important; border-radius: 3px; color: #adb2b2 !important; font-size: 13px; line-height: 13px; padding: 2px 6px; text-decoration: none;">SEO:    </a> </div>
      
      





For the layout of blog letters, “Pechkin” was used - a gulp-collector of letter templates that collects a letter from blocks and inlines the styles itself. You can also use online style inlining, for example, Premailer.io .







A feed loop is available in the RSS feed template. Therefore, you can pull and shape everything as you want and need.







The first approach to the solution was partial: leave the title, url, brief description tags as standard, but post meta-information: views, likes, comments, category and publication date, form in a separate piece of HTML code and paste in the markup place.







This option also had to be abandoned due to a broken layout and lack of control over the layout and image styles. And later transfer all the markup of the post card block to the <description>



.







 <?php while (have_posts()): the_post(); ?> <item> <description><![CDATA[ <div class="post-card"> <?php if ( get_post_meta($post->ID, 'imga', true) ) { ?> <div class="post-card__img"> <a href="<?php the_permalink(); ?>?utm_source=newsletter" target="_blank"> <img src="<?php echo $postImg[0]; ?>" alt="<?php the_title(); ?>"> </a> </div> <?php } ?> <div class="post-card__info"> <h2> <a href="<?php the_permalink(); ?>?utm_source=newsletter"><?php the_title(); ?></a> </h2> <p> <a href="<?php the_permalink(); ?>?utm_source=newsletter"> <?php if ( !empty( get_post_meta($post->ID, 'intro', true) ) ) { echo get_post_meta($post->ID, 'intro', true); } else { $content = get_the_content(); $trimmed_content = wp_trim_words( $content, 12, '...' ); echo $trimmed_content; } ?> </a> </p> <div> <p> <?php if (get_the_date('Y') == date('Y')) { the_time('j F'); } else { the_time('j F Y'); } ?>, <?php $categories = get_the_category(); if( $categories[0] ) { echo '<a href="' . get_category_link($categories[0]->term_id ) . '?utm_source=newsletter">'. $categories[0]->name . '</a>'; } ?> <?php if(function_exists('the_views')) { ?> <span> <span> <img src="icon-views.png" alt=" "> </span> <span class="item__text"><?php the_views(); ?></span> </span> <?php } ?> <?php if (function_exists('get_simple_likes_counter')) { if (get_simple_likes_counter( get_the_ID() ) >= 1) { ?> <span> <span class="item__icon"> <img src="icon-like.png" alt=" "> </span> <span><?php echo get_simple_likes_counter( get_the_ID() ); ?></span> </span> <?php } } ?> <?php if (get_comments_number() >= 1) { ?> <span> <span> <img src="icon-comments.png" alt=" "> </span> <span><?php comments_number('0', '1', '%'); ?></span> </span> <?php } ?> </p> </div> </div> </div> ]]></description> </item> <?php endwhile; wp_reset_query(); ?>
      
      





There are no inline styles in the sample code to make everything look neat. The combat version of the tape is given with full markup and styles.







All addresses for graphics in the example are also simplified. In the combat version, all the necessary pictures, icons, photographs, and everything that is important for writing are uploaded to the MailChimp admin panel via Content Studio , and the templates already src-



full-address for src-



attributes of <img />



tags.







In lines No. 20-24, we select the version of the text-announcement (intro) of the article. If the custom is filled in the post properties in arbitrary fields of the record - get_post_meta () , then show it, if it is not, then show what lies in get_the_content () .







Also select a post cover image. We have two pictures associated with the article: a small preview (displayed in the post card on the main page) and a cover from the post header. Take a big cover.







Custom MailChimp auto-feed from RSS feed






An example of a header with a background in a PromoPult blog post







Since all this happens in the standard WordPress cycle, all sorting methods are available. For example, show the most viewed and liked in the last 12 days, sorting from more to fewer likes.







Output content from the feed in the email template



Since all the data for a separate post card is located inside the <description>



tag embedded in a separate <item>



element, you just need to show it in the message template:







 <h1>     PromoPult</h1> <p>*|RSSFEED:DESCRIPTION|*</p> <!-- *|RSSFEED:DESCRIPTION|* : «21  — 28 »,   <description>   --> *|RSSITEMS:|* *|RSSITEM:CONTENT|* *|END:RSSITEMS|*
      
      





The data is displayed correctly, the HTML code is inserted in a loop, and auto-campaign works.







Separate line about RSSFEED and FEEDBLOCK



What is the difference between *|RSSFEED|*



and *|FEEDBLOCK|*?









The differences are easy to figure out:









TOTAL: If everything is standard and easy for you, use standard MailChimp tools and do not worry



But if your blog design and mailing lists are specially designed and coordinated three times with a fight, if you want to add the available data to auto-emails or at least decline the date in Russian, you will have to bother a bit and send ready-made packed data to MailChimp.







How - we told.







Links on the topic of RSS feeds in MailChimp:






All Articles