Quantcast
Viewing all articles
Browse latest Browse all 5

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.

{code type=php}
function myGreatPluginRSSFeed() {
// Function content goes here.
}
?>
{/code}

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.

{code type=php}
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/">







{/code}

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.
{code type=php}
$rssItems = array(
‘item_1′=>array(
‘Title’,
‘Link’,
‘Author’,
‘Date’,
‘Description’,
‘Content’
),
‘item_2′=>array(
‘Title’,
‘Link’,
‘Author’,
‘Date’,
‘Description’,
‘Content’
)
);
{/code}

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

{code type=php}
foreach ($rssItems as $item) {
echo ‘‘;
echo ‘‘;
echo ‘ ‘ . $item['Link'] . ‘‘;
echo ‘‘ . $item['Author'] . ‘‘;
echo ‘ ‘ . mysql2date(‘D, d M Y H:i:s +0000′, $item['Date']) . ‘‘;
echo ‘‘ . $item['Description'] . ‘‘;
echo ‘‘;
echo ‘
‘;
}
{/code}

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:
{code type=php}
// Add our new RSS feed.
add_feed('guestbook', 'myGreatPluginRSSFeed');
?>
{/code}

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.


Viewing all articles
Browse latest Browse all 5

Trending Articles