How to Create Custom RSS Feeds

RSS feeds are very versatile, they can be easily used to syndicate custom content to partner websites, rather than just posts.

Examples of sites which use RSS feeds in this way are iTunes and Flickr.

The question is how can you generate such custom feeds from your WordPress websites? Turns out there isn’t a plugin (ok, just one, that’s gone over 2 years without updating) that can help you out with this, you’ll have to do some diving into code or simply have one of our partners do it for you.

Lets Take a Deeper Dive

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 a 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.

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

  • Title
  • Link
  • Published Date
  • Author
  • Excerpt

First, 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');
functioncustomRSS(){
        add_feed('feedname','customRSSFunc');
}

The above code triggers the customRSS function, which adds the feed. The add_feed function has two arguments, feed name, 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 feed name, 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:

functioncustomRSSFunc(){
        get_template_part('rss','feedname');
}<br>

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):

  1. wp-content/themes/child/rss-feedname.php
  2. wp-content/themes/parent/rss-feedname.php
  3. wp-content/themes/child/rss.php
  4. 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/"
        <?phpdo_action('rss2_ns');?>>
<channel>
        <title><?phpbloginfo_rss('name');?>-Feed</title>
        <atom:link href="<?phpself_link();?>"rel="self"type="application/rss+xml"/>
        <link><?phpbloginfo_rss('url')?></link>
        <description><?phpbloginfo_rss('description')?></description>
        <lastBuildDate><?phpechomysql2date('D, d M Y H:i:s +0000',get_lastpostmodified('GMT'),false);?></lastBuildDate>
        <language><?phpechoget_option('rss_language');?></language>
        <sy:updatePeriod><?phpechoapply_filters('rss_update_period','hourly');?></sy:updatePeriod>
        <sy:updateFrequency><?phpechoapply_filters('rss_update_frequency','1');?></sy:updateFrequency>
        <?phpdo_action('rss2_head');?>
        <?phpwhile(have_posts()):the_post();?>
                <item>
                        <title><?phpthe_title_rss();?></title>
                        <link><?phpthe_permalink_rss();?></link>
                        <pubDate><?phpechomysql2date('D, d M Y H:i:s +0000',get_post_time('Y-m-d H:i:s',true),false);?></pubDate>
                        <dc:creator><?phpthe_author();?></dc:creator>
                        <guid isPermaLink="false"><?phpthe_guid();?></guid>
                        <description><![CDATA[<?phpthe_excerpt_rss()?>]]></description>
                        <content:encoded><![CDATA[<?phpthe_excerpt_rss()?>]]></content:encoded>
                        <?phprss_enclosure();?>
                        <?phpdo_action('rss2_item');?>
                </item>
        <?phpendwhile;?>
</channel>
</rss>

This template code will generate an RSS feed following the above layout. The postCountvariable 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_feedfunction
    • 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.

      global $wp_rewrite;
      $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
  • I’m getting a <language /> validation error!
    • 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’sfunctions.php file, to update the language option
functionrssLanguage(){
        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.

Source: WP Beginner

Still need help? Contact Us Contact Us