Skip links
Custom RSS Feed in WordPress

How to Create Custom RSS Feed in WordPress

Do you want to set up a custom RSS feed in WordPress? WordPress includes a default RSS feed that shows your website’s content. However, if you have a specific type of information, you might consider creating a unique RSS feed for it.

In this article, we will demonstrate how to simply establish a custom RSS feed in WordPress.

Why Create a Custom RSS Feed in WordPress?

WordPress includes an RSS feed, to which you can easily add custom content or pictures. However, if your WordPress website contains a specific type of content or post type, you should consider creating a unique RSS feed for it. For example, if you provide your subscribers with exclusive content from individual authors.

You may establish a customized RSS feed to keep them updated on the content. Furthermore, customized feeds give you greater flexibility by allowing you to include the author’s name, publication dates, or any other important information, offering more information to your users. Having stated that, let’s go over how to simply generate custom RSS feeds in WordPress step by step.

How to Create a Custom RSS Feed in WordPress

You may easily create a custom RSS feed in WordPress by adding code to your theme’s functions.php section. This feed will show the title, link, published date, author, and excerpts from each post on your WordPress blog. However, take in mind that even the smallest error in providing this code can cause your website to crash and become unavailable.

This is why we recommend using WPCode. It is the greatest WordPress code snippets plugin on the market, making it simple and secure to add custom code to your website. First, install and activate the WPCode plugin. For more information, visit our beginner’s guide to installing a WordPress plugin.

Note: For this tutorial, you can use the free WPCode plugin. However, purchasing the subscription plan unlocks additional capabilities such as the test mode, CSS Snippet option, cloud library, conditional logic, and more.

Once activated, navigate to the Code Snippets » + Add Snippet page from the WordPress dashboard. Click the ‘Use Snippet’ button below the ‘Add Your Custom Code (New Snippet)’ option.

Add Snippet

This will direct you to the ‘Create Custom Snippet’ page, where you may begin by entering a name for the code snippet. Next, select ‘PHP Snippet’ from the Code Type dropdown menu on the right.

PHP Snippet

After that, enter the following custom code into the ‘Code Preview’ box.

add_action('init', 'customRSS');
function customRSS(){
        add_feed('Feed', 'customRSSFunc');
}function customRSSFunc(){
        get_template_part('rss', 'feedname');
}<?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>

You can now modify the name of your custom RSS feed to anything you want by replacing the Feed text following the add_feed line in the code.

Once you’ve generated this feed, you can access it via the URL yourdomain.com/feed/feedname.

Custom RSS Feed

Next, scroll down to the insertion area and select ‘Auto Insert‘ mode.

When you activate the code, it will immediately produce a customized RSS feed.

Auto Insert

Finally, scroll back to the top and change the ‘Inactive‘ switch to ‘Active‘.

After that, click the ‘Save Snippet‘ button to save your settings.

Save Snippet

Once completed, navigate to the Settings » Permalinks tab in the WordPress dashboard.

Simply click the ‘Save Changes’ button to reset the rewriting rules and create new permalinks for your website.

Permalinks

You may now read your custom RSS feed at yourdomain.com/feed/feedname.

Bonus: Troubleshooting Custom RSS Feed Problems

Here’s a summary of some common issues that might arise while attempting to establish a custom RSS feed, as well as solutions to them. I receive a 404 error while trying to view my feed!

If you receive the 404 error when attempting to view your custom feed, ensure that you are using the right feed name in your URL. It must be the one you specified in the add_feed function. If you entered the right feedname, your rewrite rules may not have been flushed properly. You can easily fix this by resaving your permalinks.

global $wp_rewrite;
$wp_rewrite->flush_rules()

My feed is not validating. The W3C provides a feed validation service that allows you to validate the generated feed and provides particular information about where your feed is not validating.

These concerns can then be simply resolved by editing the code line. I’m receiving a validation error! This is frequent when your WordPress installation’s RSS language is not specified. For this, enter the following code into WPCode to adjust the language option:

function rssLanguage(){
        update_option('rss_language', 'en');
}
add_action('admin_init', 'rssLanguage');

After that, alter the second argument of the update_option method to the language you require. Check out the complete list of RSS Language Codes.

When you save the snippet, the problem will be fixed, and you will be able to access your customized RSS feed.

Also, Read More Relevant Articles

The Last Conclusion

Finally, generating custom RSS feeds in WordPress can significantly improve the flexibility and functionality of your website. You can customize your RSS feeds to provide specific material to your audience using plugins or custom code, whether it’s filtered by category, tag, author, or other parameters.

This not only improves user experience but also allows you to target your content to the correct demographic, increasing interaction and perhaps driving more visitors to your website. With the step-by-step instructions offered, you can confidently customize your RSS feeds in WordPress, maximizing the potential of this powerful information syndication tool.

Leave a comment

This website uses cookies to improve your web experience.
Home
Account
Cart
Search
Explore
Drag