Create an RSS Feed with WordPress
Posted by Frank on October 6, 2008When 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.
Hi Frank,
This is great, I was wondering how to build my own rss feed for one of my plugins. I googled and found this post of yours.
Really well explained.
Thanks!
Is there any way to make this into a widget instead? That’s kind of what I’m looking for.
Pingback: Research for Mailchimp to Wordpress RSS | nhaskins.com
yaşlılarınızı güvenle emanet edebilirsiniz
Thanks AGAIN!
This is what I’ve been trying to do myself!
Vi driver datahjelp og pchjelp i Oslo.
I love your site, dude!
perfect thank you admin
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.
Thanks for the info. I’ve been trying to create or modify the built in feed and this look like a better solution!
perfect thank you admin
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
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
Hey, thanks for the post, just what Ive been looking for.
ohh thanks. I will try
Pingback: Liens du 24/11/2008
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