Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

[WordPress] Create Custom RSS Feeds

Hi,

WordPress is giving default RSS feed which is enough for most of the users who want to share their feed with other people (publicly). You can find your default feed url byt writing /feed after your domain url.

e.g.
http://www.programmingfacts.com/feed/

In some cases, you want to do little customization like adding thumbnail to RSS feed or adding custom content to existing default feeds. This can be achieved using some plugins as well as with some sort of code. But what if you want more than this ? Are you looking for creating your own Custom RSS feed for your articles ? If yes, here is detailed instructions how to create custom RSS feeds in WordPress.

Step 1

First you need to define your new RSS feed in your functions.php file. It will set up a URL so you can give name accordingly.

add_action('init', 'pf_my_own_rss');
function pf_my_own_rss(){
add_feed('customfeed', 'pf_my_own_rss_func');
}

With the above code, your custom RSS feed url will look like below.
http://www.yourdomain.com/feed/customfeed

Step 2

Now you need to write callback function. Write below code in functions.php file. You can either write code directly in below function or make separate file and call the file from here using get_template_part function.

function pf_my_own_rss_func(){
        get_template_part('rss', 'mycustomfeed');
}

 

Step 3

Now you need to create rss-mycustomfeed.php file under your theme directory. Write the below code and change it according to your needs.

<?php
/**
 * Template Name: Custom RSS Template - My Own Feeds
 */
$postCount = 25;
$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: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 edit it and add custom content whatever you would like to do as per your requirement.

Step 4

In order to make url works, you need to flush the rewrite rules. You can do it easily by going to Settings -> Permalinks and Click on Save. You are done!

Step 5

If you want to validate your RSS Feed, you can do using feed validation service, allowing you to validate the resulting feed.

Share: