Create an RSS Feed with WordPress

When you are busy creating a wonderful plugin for your WordPress blog, it might be possible that you need to create a RSS feed. For example, when creating a guestbook people could ask for a RSS feed to view the recent added posts and stay updated, or take a look at my own Community News Plugin, isn’t it great if it had a build-in RSS feed as well?

Well don’t worry I am busy to create that and it is almost finished. In the mean while, I wanted to share some stuff I learned while creating this RSS feed.

The Beginning

The first thing we need is a function that actually creates the RSS feed. This function doesn’t need any parameters and it also shouldn’t return anything.

<?php
function myGreatPluginRSSFeed() {
    // Function content goes here.
}
?>

This function must create the whole feed form the beginning to the end. So, you also have to include the xml/rss header. I ripped something from the default WordPress feed that should work for any blog.

<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/">
    
    <channel>
        <title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
        <link><?php bloginfo_rss('url') ?></link>
        <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
        <description><?php bloginfo_rss("description") ?></description>
        <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>
        
        <item>
        </item>
        
    </channel>
</rss>

Now, you could see the <item> and </item> tags, that is where we are going to add our feed items. From this moment I assume that all our RSS data is located in an array like this one.

<?php
$rssItems = [
    'item_1' => [
        'Title',
        'Link',
        'Author',
        'Date',
        'Description',
        'Content'
    ],
    'item_2' => [
        'Title',
        'Link',
        'Author',
        'Date',
        'Description',
        'Content'
    ]
];

And so on. For adding the items to our feed, we simply use a foreach loop.

<?php
foreach ($rssItems as $item) {
    echo '<item>';
    echo '<title>' . $item['Title'] . '</title>';
    echo '<link>' . $item['Link'] . '</link>';
    echo '<dc:creator>' . $item['Author'] . '</dc:creator>';
    echo '<pubDate>' . mysql2date('D, d M Y H:i:s +0000', $item['Date']) . '</pubDate>';
    echo '<description>' . $item['Description'] . '</description>';
    echo '<content:encoded><![CDATA[' . $item['Content'] . ']]></content:encoded>';
    echo '</item>';
}

The mysql2date function is a WordPress function that converts our date to the format we want for our feed. The input should be the date in the following format: d-m-Y H:i:s. See php.net for more information about the date formatting.

Now we have finished our function, we have only one thing to do.

Adding the RSS Feed to WordPress

To add the feed, we could use one simple function, build-in in WordPress since version 2.1.0. This is the add_feed function. This function has two parameters.

  • $feedname; The name of your feed.
  • $function; The function that creates the feed. (The one we created above)

So lets say we name our feed: ‘guestbook’, then we should call the add_feed function this way:

<?php
// Add our new RSS feed.
add_feed('guestbook', 'myGreatPluginRSSFeed');
?>

Now our feed is added and working! One thing you might be asking yourself is: Where could I find my feed? Well, that’s simple.
If you are using permalinks on your blog (could be checked this way: $wp_rewrite->using_permalinks()), then your feed is located at: http://your.website.com/[feedname].
If you don’t use permalinks, your feed is located at: http://your.website.com/?feed=[feedname].

Done!

After all our hard work, we have created one beautiful RSS 2.0 feed, working with WordPress. Any questions? Please leave a comment.

PHP, WordPress
,