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.