Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Last active January 27, 2019 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finalwebsites/7bf60b84468be304d8f171e2bb36de55 to your computer and use it in GitHub Desktop.
Save finalwebsites/7bf60b84468be304d8f171e2bb36de55 to your computer and use it in GitHub Desktop.
Page template to create RSS campaigns with WordPress and Mailchimp
<?php
/*
Template Name: Custom RSS Feed
*/
$numposts = 5;
$numchars = 300;
$thumbformat = 'medium';
$alt_text = 'Klik hier voor de afbeelding...';
function the_excerpt_max_charlength($excerpt, $charlength) {
$charlength++;
$str = '';
if ( mb_strlen( $excerpt ) > $charlength ) {
$subex = mb_substr( $excerpt, 0, $charlength - 5 );
$exwords = explode( ' ', $subex );
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
if ( $excut < 0 ) {
$str .= mb_substr( $subex, 0, $excut );
} else {
$str .= $subex;
}
$str .= '...';
} else {
$str .= $excerpt;
}
return $str;
}
$posts = query_posts(array('posts_per_page' => $numposts));
$lastdate = date(DATE_RSS, strtotime($posts[0]->post_modified_gmt) );
header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss">
<channel>
<title>'.get_option('blogname').' RSS Updates</title>
<link>'.get_option('home').'</link>
<description>The latest blog posts from '.get_option('blogname').'.</description>
<language>'.strtolower(get_bloginfo('language')).'</language>
<pubDate>'.$lastdate.'</pubDate>
<lastBuildDate>'.$lastdate.'</lastBuildDate>
<managingEditor>'.get_option('admin_email').'</managingEditor>';
foreach ($posts as $post) {
setup_postdata( $post );
$excerpt = get_the_excerpt();
echo '
<item>
<title>'.get_the_title().'</title>
<link>'.get_permalink().'</link>
<description><![CDATA['.the_excerpt_max_charlength($excerpt, $numchars).']]></description>
<pubDate>'.date(DATE_RSS, strtotime(get_the_date())).'</pubDate>';
if (get_the_post_thumbnail()) {
$img = wp_get_attachment_image_src(get_post_thumbnail_id(), $thumbformat);
echo '
<media:content url="'.$img[0].'" medium="image" description="'.$alt_text.'"/>';
}
echo '
<guid>'.get_permalink().'</guid>
</item>';
}
wp_reset_postdata();
echo '
</channel>
</rss>';
@finalwebsites
Copy link
Author

Place this template file into your theme directory and create a simple page without any content.
Tip! Use Yoast SEO's "noindex" feature and exclude that page from Google.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment