WordPress Cyber Monday Deals 2021!

How to Create Custom RSS Feeds in WordPress

With the abundance of news pieces and the number of blog sites continuously growing, keeping track of what’s coming round can be tiring. Fortunately, there is such a standard as RSS that makes it super easy. It provides a simple yet an effective way to keep up with the latest news and what matters to you most. In other words, RSS grabs content from a particular website and aggregates it into a news reader application.

With the abundance of news pieces and the number of blog sites continuously growing, keeping track of what’s coming round can be tiring. Fortunately, there is such a standard as RSS that makes it super easy. It provides a simple yet an effective way to keep up with the latest news and what matters to you most. In other words, RSS grabs content from a particular website and aggregates it into a news reader application.

When implemented well, this automation solution saves readers time greatly. Really Simple Syndication monitors the websites that you’re interested in and synchronises all their new content updates as they come out. It takes the daunting search routine on its part, leaving you the time just to glimpse and enjoy the latest.  

You can use these feeds in a number of ways. For very basic implementation, WordPress has its built-in default RSS feeds. But there are also more specialized purposes for delivering a more specific type of content, for which you may need to create a custom RSS. In this article, we’ll show you how to do this.

Please note that this tutorial is not targeted for beginner level of WordPress users.

As always, before making any major changes to a live website, remember to complete your WordPress website backup.

The first thing you need to do is initialize your custom RSS action in your theme’s functions.php file.

add_action( 'init', 'myRSS' );
function myRSS() {
  add_feed( 'feedname', 'myRSSFeed' );
}

The function above adds the feed to the template through the add_feed function. The add_feed function has two arguments, feedname, and a callback function. The feedname will make up your new feed URL yourdomain.com/feed/feedname, and the callback function will be called to actually create the feed. So, we need to create the myRSSFeed function in theme’s functions.php file.

You can place the RSS code directly in the callback function. By using get_template_path, we can keep the functionality separate to the layout.

function myRSSFeed() {
  get_template_path( 'slug', 'name' );
}

The get_template_path function takes the slug and name arguments to a template file like wp-content/themes/themename/slug-name.php or wp-content/themes/themename/slug.php.

Take a look on the template file.

<?php
/**
 * RSS Feed Template by Julia N.  
 */
$limitCount = 7; // The posts limit to show
$posts = query_posts('showposts=' . $limitCount);


// Setting up content type and charset headers 
header('Content-Type: '.feed_content_type('rss-http').';charset='.get_option('blog_charset'), true);

// Setting up valid XML encoding
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?> 

<!-- Declaring XML Validators namespaces -->
<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/"
        <?php do_action('rss2_ns'); ?>>
<!-- Declaring channel with articles data --> 
<?php $dateTimeFormat = 'D, M d Y H:i:s'; ?>
<channel>
        <title><?php bloginfo_rss('name'); ?> - Feed</title>
        <link><?php bloginfo_rss('url') ?></link>
        <description><?php bloginfo_rss('description') ?></description>
        <lastBuildDate><?php echo mysql2date($dateTimeFormat, get_lastpostmodified(), false); ?></lastBuildDate>
        <language><?php echo get_option('rss_language'); ?></language>
        <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'daily' ); ?></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>
                        <pubDate><?php echo mysql2date($dateTimeFormat, get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
                        <dc:creator><?php the_author(); ?></dc:creator>
                        <guid isPermaLink="false"><?php the_guid(); ?></guid>
                        <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
                        <content:encoded><![CDATA[<?php the_excerpt_rss() ?>]]></content:encoded>
                        <?php rss_enclosure(); ?>
                        <?php do_action('rss2_item'); ?>
                </item>
        <?php endwhile; ?>
</channel>
</rss>

The final thing that is left to do is flush your WordPress rewrite rules. To proceed with this action easily, you need to log in to the WordPress admin panel, and go Settings -> Permalinks. Once there, just click Save Changes, this will flush the rewrite rules. Now you can access your feed at yourdomain.com/feed/feedname.

The last thing to mention is that you can aggregate as many RSS feeds from as many sources as you’d like if you apply some ingenuity. The easiest way to do so is to make a use of WP RSS Aggregator. It is a very neat plugin for importing, merging and displaying RSS and Atom feeds on your WordPress site. This solution is very comprehensive and elegant for WordPress, so it does not take much effort and time to get started.

Julia Nimchynska
Julia Nimchynska
Marketing manager at CMS2CMS, a travel lover, simplicity enthusiast, a fan of jazz and yoga.

Consider sharing this post so others can find it:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on telegram
Share on whatsapp
Share on pocket
Share on email

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

Our Sponsors
Solid Affiliate
Contents

16 Responses

  1. Great tutorial – would be nice to expand on getting set categories or even Custom post types or attributes / tags

    1. I’m glad you like it, Scott. Thanks for your recommendations, I’ll consider them in my further posts 🙂

  2. get_template_path( ‘slug’, ‘name’ );

    That is not a function it is

    get_template_part( ‘slug’, ‘name’ );

  3. I have tried three dif types of code and they come up without a 404 but the files are blank. Any ideas?

  4. Hi,
    Thanks for your post!
    Any idea how to add a custom post type to this rss feed ?

    Regards

  5. Anybody can help me to get the total likes in feed
    currently i am getting the total commnet
    i am using the plugin kodes post likes
    which is not generating the total likes in the rss feeed

    thanks

Leave a Reply

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

The Beginner’s Handbook
From an introduction on how WordPress works to our recommendations on products and services.
👋 Hey there! We're Gaby and Mark
Every week we share tutorials and genuine reviews of WordPress products and services in our newsletter.
Thousands of people read it!
We’d love for you to join.
We’d love for you to join. Here’s what you’ll be getting:

A single weekly email directly to your inbox.