fbpx
How to Create Custom RSS Feed in WordPress

How to Create Custom RSS Feed in WordPress

WordPress comes with standard RSS feed functionality which is adequate for most users. But, if you want to add custom content or add post thumbnail to your RSS feeds, you will have to make a custom RSS feeds in WordPress.

Luckily, you can customize your RSS feed easily just by adding few lines of code. However, take note that this tutorial is not beginner-friendly, so if you want to tweak the RSS feed, first do it on a local WordPress.

Related Posts:

Also, ensure that you take the whole backup of your WordPress site prior to doing any modification to your live site.

With that being said, let’s understand what is RSS feeds and later on we will see how you can create your own custom RSS feed in WordPress.

What is an RSS Feed?

RSS stands for Really Simple Syndication. It’s really useful for bloggers who want to grow their audience as this ideal functionality allows bloggers to share their content on the other sites through feed readers.

Visitors that sign up for RSS feeds can utilize their pick feed reader to browse content, leave comments and share the posts on social media platforms.

If you want to make your content stand out, you need to create your own custom RSS feed and target specific readers.

Now, let’s see how to create a custom RSS feed in WordPress

How to Create Your Own Custom RSS Feed in WordPress?

This Guide will help you in creating a custom RSS feed with the attributes shown below:

  • Post title
  • Link
  • Published date
  • Author
  • Excerpt

Create the new RSS feed in your theme’s functions.php file. Get started by add following code snippet:

add_action('init', 'customRSS');
function customRSS(){
        add_feed('feedname', 'customRSSFunc');
}

The above code will enable the customRSS function on your website. You will have to change the ‘feedname’ according to the name of the feed, you like to be called.

Then, you are required to make a callback function, and you can do so by adding the below displayed code:

function customRSSFunc(){
        get_template_part('rss', 'feedname');
}

The above code uses the get_template_part function to link to a separate template file. But, you can likewise put the RSS code straight into the function.

We suggest you set the slug to ‘rss‘ so as to make it simpler to navigate.

The next step is to make a feed template. Make a new file in your child theme’s folder and save the file name as slug-name.php, by using the arguments from the earlier snippet.

The following code is for template file:

<?php
/**
 * Template Name: Custom RSS Template - Feedname
 */
$postCount = 5; // The number of posts to show in the feed
$posts = query_posts('showposts=' . $postCount);
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<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'); ?>>
<channel>
        <title><?php bloginfo_rss('name'); ?> - Feed</title>
        <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
        <link><?php bloginfo_rss('url') ?></link>
        <description><?php bloginfo_rss('description') ?></description>
        <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
        <language><?php echo get_option('rss_language'); ?></language>
        <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></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('D, d M Y H:i:s +0000', 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>

Note: You can manage and control the number of posts to be shown in your feed by changing the postCount variable. You can additionally tweak this template if you wish that your feed display images, comments, etc.

Once your done and satisfied with your feed template, save the template and head on to Settings → Permalinks which is accessible in your dashboard.

Click on Save Changes. This will flush the rewrite rules so that your feed can be display appropriately.

Last but not the least, you can access your custom feed at yourdomain.com/feed/feedname.

Conclusion

We hope that this blog post helped you create your own custom RSS Feeds in WordPress. 

Leave a Comment

Your email address will not be published.

Our Experts will help you build the right solution or find the right partner for your needs.