How to Create Custom RSS Feeds in WordPress

WordPress comes with built-in defaultΒ RSS feeds. You can tweak the default feeds byΒ adding custom content to your RSS Feeds, or evenΒ adding post thumbnail to your RSS Feeds. The default RSS and Atom feeds are enough for most users, but you may wish to create a custom RSS feed for delivering specific type of content. In this article, we will show you how to create custom RSS feeds in WordPress.

Please note that this tutorial is not intended for beginner level WordPress users. If you are a beginner, and still want to try it, then please do so on aΒ local install.

As always, you must create aΒ complete backup of your WordPress websiteΒ before making any major changes to a live website.

Having said that, let’s get started with your first custom RSS feed in WordPress.

Let’s assume you want to create a new RSS feed which displays just the following information:

  • Title
  • Link
  • Published Date
  • Author
  • Excerpt

First thing you need to do is create the new RSS feed in your theme’s functions.phpΒ file or in aΒ site-specific plugin:

add_action(‘init’, ‘customRSS’);
function customRSS(){
Β Β Β Β Β Β Β Β add_feed(‘feedname’, ‘customRSSFunc’);
}

The above code triggers the customRSS function, which adds the feed. 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. Make a note of the feedname, as you’ll need this later on.

Once you have initialized the feed, you’ll need to create the callback function to produce the required feed, using the following code in your theme’s functions.phpΒ file or in a site specific plugin:

function customRSSFunc(){
Β Β Β Β Β Β Β Β get_template_part(‘rss’, ‘feedname’);
}

The code above is using the get_template_part function to link to a separate template file, however you can also place the RSS code directly into the function. By using get_template_part, we can keep the functionality separate to the layout. The get_template_partΒ function has two arguments, slug and name, that will look for a template file with the name in the following format, starting with the file at the top (if it doesn’t find the first, it will move on to the second, and so on):

wp-content/themes/child/rss-feedname.php
wp-content/themes/parent/rss-feedname.php
wp-content/themes/child/rss.php
wp-content/themes/parent/rss.php

For the purposes of this tutorial, it is best to set the slug to the type of feed you’re creating (in this case: rss), and the name to the feedname configured earlier on.

Once you’ve told WordPress to look for the feed template, you’ll need to create it. The below code will produce the layout for the feed with the information we listed earlier. Save this file in your theme folder as the slug-name.php template file configured in the get_template_partΒ function.

<?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>

This template code will generate an RSS feed following the above layout. The postCountΒ variable allows you to control the number of posts to display in your feed. The template can be amended as required to display whatever information you require (e.g. post images, comments, etc).

The the_excerpt_rssΒ function will display the excerpt of each post, and for posts that do not have excerpts, it will display the first 120 words of the post’s content.

Finally, to display your feed, you’ll first need to flush your WordPress rewrite rules. The easiest way to do this is by logging in to the WordPress admin, and clickingΒ Settings -> Permalinks. Once here, just clickΒ Save Changes, which will flush the rewrite rules.

You can now access your new feed at yourdomain.com/feed/feedname, where feedname was the feedname you gave in the add_feedΒ function earlier on.

The W3C offers aΒ feed validation service, allowing you to validate the resulting feed.

Troubleshooting

  • I’m getting a 404 error when trying to view my feed!
      • Check to see if you are using the correct feedname in your URL. It has to be the one you supplied in the add_feedΒ function
      • If you have the correct feedname, your rewrite rules may not have flushed correctly. Re-save your permalinks just to be sure.
      • If you’ve re-saved your permalinks, you can force a rewrite flush via your theme’s functions.php file. Add the following code to the customRSS function we created earlier. Make sure you add the code after the add_feedΒ function.
    1 global $wp_rewrite;
    2 $wp_rewrite->flush_rules();
    Β 
    • Once you’ve added this, reload your WordPress site.Β NOTE: This should be removed immediately after use. Once is enough for the rules to be flushed.
  • My feed isn’t validating!
    • Using the W3C feed validator, specific details should be given where your feed isn’t validating. Edit the feed template file to resolve these issues
      • This is common where the RSS language has not been configured on your WordPress installation. To do this, you can add the following code to your theme’s functions.phpΒ file, to update the language option.I’m getting a <language /> validation error!
  • function rssLanguage(){
    Β Β Β Β Β Β Β Β update_option(‘rss_language’, ‘en’);
    }
    add_action(‘admin_init’, ‘rssLanguage’);
    • Edit the second argument of the update_option function to change the language to one you require. Check out the full list ofΒ RSS Language Codes.
    • Once the above code has been added to your functions file, load the WordPress admin screen for it to take effect.Β After this, the code should be removed from your WordPress functions file. Loading it once is enough to configure the rss_language setting.
    • This can also be done directly in the database, by looking for the rss_language option in the wp_options table.

We hope this article helped you create your own custom RSS Feeds in WordPress. Let us know how and why you will be using custom RSS feeds on your WordPress site by leaving a comment below.

Leave a Reply

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

error: Content is protected !!

Pin It on Pinterest

Add address