Content Marketing

How To Syndicate External RSS Feeds In Your WordPress Theme or Child Theme

Some folks don’t realize it, but WordPress has integrated the ability to syndicate RSS feeds with some out-of-the-box features. While there are widgets to do this, you may actually want to include the ability to publish other feeds directly into your WordPress template.

WordPress supports both Magpie and SimplePie RSS Caching within its available function, fetch_feed:

  • fetch_feed – retrieve an RSS feed from a URL with automatic caching

This really comes in handy if you have multiple sites and want to share your blog posts on the other sites as soon as they publish. It can also be nice from an SEO standpoint, producing backlinks on another site automatically as you publish your content.

I’ve also utilized this approach to publish podcasts and video feeds from one site to another.

WordPress Theme or Child Theme Template

// Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );
$rss = fetch_feed('https://feed.martech.zone');
if ( ! is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity( 5 ); 
$items = array_slice($rss->get_items, 0, $maxitems);
endif;
?>

<ul>
<?php if (empty($items)) echo '<li>No items</li>';
else
foreach ( $items as $item ) : ?>
<li><a href='<?php echo esc_url( $item->get_permalink() ); ?>' 
title='<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>'>
<?php echo esc_html( $item->get_title() ); ?>
</a></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

If you publish and don’t immediately see your new post on another site, keep in mind that fetch_feed caches for 12 hours by default. You can modify this by modifying the time interval via the filter wp_feed_cache_transient_lifetime.

function update_cache_time( $seconds )
{
// change the default feed cache recreation period to 1 hour
return (int) 3600;
}

//set feed cache duration
add_filter( 'wp_feed_cache_transient_lifetime', 'update_cache_time');

If you’d like to update the cache for a specific feed, you can apply the filter, fetch the feed, and then reapply the default cache time by updating your code as follows:

// filter to set cache lifetime
add_filter( 'wp_feed_cache_transient_lifetime' , 'update_cache_time' );

$rss = fetch_feed( $feed_url );

// reset the cache lifetime to default value
remove_filter( 'wp_feed_cache_transient_lifetime' , 'update_cache_time' );

Edit your WordPress template (Design > Theme Editor) and place the code where you’d like the feed to publish. There are also a ton of sidebar widgets out there that will publish feeds for you as well.

Douglas Karr

Douglas Karr is the founder of the Martech Zone and a recognized expert on digital transformation. Douglas has helped start several successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to launch his own platforms and services. He's a co-founder of Highbridge, a digital transformation consulting firm. Douglas is also a published author of a Dummie's guide and a business leadership book.

4 Comments

    1. Good to see you on the circle, Stephen! Curious of the advantages of the plugin over the built in features? Is it simply the interface? I like the fact that caching is built into the internal function – for high volume days/sites, that could come in handy!

      Cheers!
      Doug

  1. Brilliant – this is exactly what I need! I’ve been working on a WP MU site and I simply want the main blog to just show a page with a nice graphic for each of the blogs. Now, I can add in a couple posts under each graphic rather than have them hang out on the sidebar using the RSS widget.

    1. Awesome, William!

      The funny part is I was half-way down the road of implementing MagPie when I decided to check the WP site out to see how others might be doing it. Those folks at WP really do a fantastic job, don’t they?

      Doug

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles