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.

$rssItems = array(
	'item_1'=>array(
		'Title',
		'Link',
		'Author',
		'Date',
		'Description',
		'Content'
		),
	'item_2'=>array(
		'Title',
		'Link',
		'Author',
		'Date',
		'Description',
		'Content'
		)
	);

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


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.

Tagged: , .

14 Responses

  1. Jay Says:
    October 31st, 2008 at 9:54 pm

    Hi, Thanks for this. Any idea how to add tags to a wordpress feed manually? for instance I see for the post id ( this is in the xml file) is there are ?

    Any help would be appreciated. Thanks

  2. Liens du 24/11/2008 Says:
    November 27th, 2008 at 5:08 pm

    [...] Créer un flux RSS pour wordpress : utiliser les fonctions de WordPress pour céer un flux RSS personnel. Tags: creation logo, wordpress plugin [...]

  3. haber Says:
    January 8th, 2009 at 12:09 pm

    ohh thanks. I will try

  4. Mr. Saturated Says:
    April 28th, 2009 at 10:51 pm

    Hey, thanks for the post, just what Ive been looking for.

  5. tkm Says:
    May 16th, 2009 at 3:55 pm

    Hi Frank,

    This is exactly what I’m looking for! I’m having a few issues. I’m trying to create the function and I’ve added the exact same code as you’ve added inside the function.

    However, I am receiving this error “Parse error: syntax error, unexpected ‘<’ “. It is complaining about the first rss tag inside the function i.e. “<rss version…”. What do you think the problem is?

    Thanks,
    tkm

  6. bayrak Says:
    June 10th, 2009 at 9:31 am

    Bayrakajans olarak Türk bayrağı imalatı toptan ve perakende satışlarında size ucuz ve kaliteli hizmet etmek için varız.Türk bayraklarımız fabrikasyon baskılı olup, büyük ebat bayraklar pikoludur. Her boyutta Atatürk pos-teri ve özel posterlerde istediğiniz ölçüler size kısa süreler içinde hızlı ürün teslimatı yaparız.Atatürk posterlerimiz yüksek çözünürlükde bayrak baskı merkezimizde basılmaktadır

  7. vistswins Says:
    August 23rd, 2009 at 6:32 am

    http://drugstore.gd/pharmacy.jpg
    free downloadable treo medical software
    brain on drugs commercial testo rex scott and white health insurance bladder support cats glans penis is dry no smegma http://legalrxdrugstore.com/group/antibiotics.html wonder girls a sorru heart lyrics http://pharmacyrxworld.info/item.php?id=5188
    health foundation association keftab the number of chemotherapy treatments for breast cancer antiox cats shoot osama bin laden game http://legalrxdrugstore.com/item/women_s_intimacy_enhancer.html california department ofo public health http://legalrxdrugstore.com/search.php?letter=Y&cv=po&lng=uk

  8. promosyon Says:
    August 31st, 2009 at 7:55 pm

    perfect thank you admin

  9. Matthew Phillips Says:
    October 8th, 2009 at 7:36 pm

    Thanks for the info. I’ve been trying to create or modify the built in feed and this look like a better solution!

  10. jazzycristina Says:
    December 5th, 2009 at 2:32 pm

    i have been trying really hard to create rss feed on one of my sites. have set up all the pages as static. but there is no way i am able to get an rss feed am i missing something here.

  11. tatil otelleri Says:
    February 25th, 2010 at 5:35 pm

    perfect thank you admin

  12. Øystein Datahjelp Soteland Says:
    March 10th, 2010 at 11:16 am

    Thanks AGAIN!
    This is what I’ve been trying to do myself!
    Vi driver datahjelp og pchjelp i Oslo.

    I love your site, dude!

  13. bakımevi Says:
    March 24th, 2010 at 5:04 pm

    yaşlılarınızı güvenle emanet edebilirsiniz

  14. Research for Mailchimp to Wordpress RSS | nhaskins.com Says:
    May 3rd, 2010 at 6:07 pm

Leave a Reply