How to create your own rss feed

RSS is a way of easily keep your site's readers up to date with your latest content. That's a reason why it is widely used by the famous blogs and big sites. Once someone subscribes to an RSS feed, he'll get a notification every time you post some new content to your site. It helps fidelizing your users, therefore improving your traffic. It is definitely worth implementing. This tutorial will guide throught the steps involved in the creation of your own RSS feed from scratch, and based on your site's existing platform.

How to setup

Step 1: Create the Channel

The feeds are not going to deliver themselves to the user, they are wrapped into what is called a channel. So before you provide the user with any actual feed content, you'll need to give him a few standard extra information. This is how you can create your own channel, it's actually just an xml element which you'll give a nice title, a link, and a handy description (you can also specify another language than English inside the language tag):
  1. <?php
  2. header("content-type: text/xml"); //Tells the browser that we are going to write some xml with php
  3. ?>
  5. <rss version="0.91">
  7. <!--Start the channel definition-->
  8. <channel>
  10. <title >jdmweb</title ><!--Channel Title-->
  11. <link></link><!--Channel Link-->
  12. <description><!--Channel Description-->
  13. is a website about web development,
  14. website creations and handy web tools for developpers
  15. by Jeremy Desvaux de Marigny.
  16. </description>
  17. <language>en-us</language><!--And finally the language-->
  19. </channel>
  20. </rss>
That's it for your channel definition! It's pretty straight forward. If you'd like, you can add an illustrative picture alongside your channel definition to make it look better, let's see how you can do that.

Step 2: Add an illustrative picture

There's nothing difficult here either really. Right after the channel description and language, we add an image xml node. This node contains a title node (the title of your image), a url node (which contains the location of your image), a link node (where to go when you click the picture), and finally the height and width nodes to specify the dimensions of your image. The code therefore becomes:
  1. <?php
  2. header("content-type: text/xml"); //Tells the browser that we are going to write some xml with php
  3. ?>
  5. <rss version="0.91">
  7. <!--Start the channel definition-->
  8. <channel>
  10. <title >jdmweb</title ><!--Channel Title-->
  11. <link></link><!--Channel Link-->
  12. <description><!--Channel Description-->
  13. is a website about web development,
  14. website creations and handy web tools for developpers
  15. by Jeremy Desvaux de Marigny.
  16. </description>
  17. <language>en-us</language><!--And finally the language-->
  19. <image> //The code to add your image goes here
  20. <title >jdmweb</title>
  21. <url></url>
  22. <link></link>
  23. <width>57</width>
  24. <height>57</height>
  25. </image>
  27. </channel>
  28. </rss>

Step 3: Add your items

Now that your RSS channel is ready, it's time to feed in your elements. Those elements have to follow a very particular structure, defined by standards. You can find more details about the full RSS 2.0 specification on the RSS Advisory Board's website In a nutshell, every RSS element will be defined as an xml item node, which can have the following child nodes:
RSS Item Required Elements:
  • title - The title of the item, for ex: Venice Film Festival Tries to Quit Sinking
  • link - The url of the item, for ex:
  • description - The item synopsis, for ex: Some of the most heated chatter at the Venice Film Festival this week was about the way that the arrival of the stars at the Palazzo del Cinema was being staged.
RSS Item Additional Optional Elements:
  • author - Email address of the author of the item
  • category - Includes the item in one or more categories
  • comments - URL of a page for comments relating to the item
  • enclosure - Describes a media object that is attached to the item
  • guid - A string that uniquely identifies the item
  • pubDate - Indicates when the item was published
  • source - The RSS channel that the item came from
Knowing these constraints, we'll be able to dynamically select the latest posts from a database, and convert them in RSS items. For the purpose of this example, we'll assume that we are working with a standard 'post' table that contains the following fields:
The 'post' table structure:
  • post_id - Unique id of the post
  • post_title - The post Title
  • post_text - The post content
  • post_thumb - Url of the post illustrating picture
  • post_category - The post Category
  • post_date - The post Date
We can now query the database to select the latest posts from this table. We'll then loop through the returned rows, and build a corresponding RSS item for each:
  1. <?php
  2. //Items dynamic building
  3. $sql="SELECT * FROM post ORDER BY post_date DESC LIMIT 10"; //SQL Query
  4. $req = mysql_query($sql) or die("Erreur SQL !: ".$sql." - ".mysql_error());
  5. while($post= mysql_fetch_assoc($req)) { //Loop throught the results
  6. echo' //Build the item
  7. <item>
  8. <title >'.htmlentities($post[post_title]).'</title> //With the title
  9. <link>http://www.yourwebsite/post_'.$post[post_id].'</link> //Link
  10. <description> //And the description
  11. '.htmlentities($post[post_text],ENT_QUOTES).'
  12. </description>
  13. </item>';
  14. }
  15. ?>
As you can see on the previous php code, on line 1, we create the SQL query that will select the 10 most recent posts based on their date. The query is sent to the database on line 2. On line 3 we start the loop through the results and from line 5 to 11 the item xml element is built from each post record. I'd just add a little note on the fact that the title and the description go through the htmlentities() function in order to strip any unwanted character prohibited in xml, such as &, ", ', > or < for example. When adding this loop into our existing code, our RSS generator now looks like this: [php] //Channel Definition jdmweb is a website about web development, website creations and handy web tools for developpers by Jeremy Desvaux de Marigny. en-us // Illustrating Image jdmweb 57 57 '; '.htmlentities($post[post_title]).' //With the title http://www.yourwebsite/post_'.$post[post_id].' //Link //And the description '.htmlentities($post[post_text],ENT_QUOTES).' '; } ?> [/php] With this code, you now have a feed generator working in relationship with the posts you save in the database. As a result, as soon as you post a new article on your site or blog, your RSS subscribers will be notified. This is the minimum required for the feed to work. Let's now have a look at a few things you can do to improve it.

How can I be sure that my feed is valid and working?

Now that the code has been written, we need to test it. To do this, save this code as a file, for example, myrss.php, and upload it to your website in a location you can access, for example,, you can find mine here: RSS If you can view it without error, then it's a good start. We'll now have your feed tested by an impartial and intransigent judge: The Feed Validator, which you can find at the following url: You just have to submit your feed url and if you see the message: "Congratulations! This is a valid RSS feed.", then you guessed it, your feed is valid. If you are still not sure, you can finally subscribe to your own feed and monitor its behaviour as time goes by.

How can I tell the browser to locate and recognize my RSS feed?

You've probably noticed that when you browse a site that has its own RSS feed, the browser detects it to allow you to either view it or subscribe to it easily. But it doesn't do it by default though, you'll have to add this simple line of code in your page header to make it work:
  1. <link rel="alternate" type="application/rss+xml" title="My Feed" href="/myrss.php"/>

How Can I add a picture to each of the items?

There is a way to illustrate each item with a thumbnail. Let's go back to our item description to modify it:
  1. <?php
  3. //Items dynamic building
  4. $sql="SELECT * FROM post ORDER BY post_date DESC LIMIT 10"; //SQL Query
  5. $req = mysql_query($sql) or die("Erreur SQL !: ".$sql." - ".mysql_error());
  6. while($post= mysql_fetch_assoc($req)) { //Loop throught the results
  7. echo' //Build the item
  8. <item>
  9. <title >'.htmlentities($post[post_title]).'</title > //With the title
  10. <link>http://www.yourwebsite/post_'.$post[post_id].'</link> //Link
  11. <description> //And the description
  12. <!&#91;CDATA&#91;
  13. <img align="left" hspace="10" src="'.$post&#91;post_thumb&#93;.'" />
  14. '.$post&#91;post_text&#93;.'
  15. &#93;&#93;>
  16. </description>
  17. </item>';
  18. }
  20. ?>
The lines 12 to 15 have been changed here compared to the previous version. We are using a special tag called CDATA, in which we place our image and the description. This will place the image specified in the post_thumb attribute in front of the post description. You can use the align and hspace properties to control the elements alignment, and change the layout.


Once you know the RSS markup specifications, you are one query away from your own custom RSS feed. By following those simple steps, you can use your blog or site existing setup to generate a feed that visitors will be able to use to stay tuned with your work. So there you go, create your channel, illustrate it with a picture, add your items, and spread the word about your site's latest addition.

Leave a Reply

Your email address will not be published. Required fields are marked *

Other Useful Resources

How to make SEO friendly urls illustration

It's now quite common to swap the old url format containing file extensions and non explicit query strings by SEO friendly ones, only made of keywords and slashes, hence their name.

There are several techniques to do so, and I certainly do not have the pretention to say that this one is the best of all, but it is the one I use in personal projects as it works nicely, and it is robust and dynamic.

It makes use of an .htaccess file to rewrite the urls, of a php function to parse the given parameters, and a content manager to serve the appropriate content according to them.

Before we start I'd like to make a personal thanks to Josh Moont for his precious help.

Read more
How to integrate the new Twitter api (1.1) in php illustration

On October 11th 2012, Twitter introduced some major changes in its API following its plans to clean things up deeply. As is explaining the twitterapi account they have for example shut down or limited access to some of their data and resources. So if you've noticed that some Twitter functionalities you are using on your site are not working any more, you may have to check a few things. In this article written on NOE interactive's blog, you'll learn what has concretely changed, and you'll be able to follow a few steps to get your Twitter feeds back.

Read more
How to Create Native Admin Tables in WordPress illustration

WordPress list tables are a very common element of the WordPress admin interface. They are used by nearly all default admin list pages, and also often implemented by developers while writing plugins. However, creating one of those tables is not really an intuitive thing to do when you haven't done it before, and I've seen cases where people where trying to replicate one, with techniques such as using the WordPress CSS classes on personal markup, or even replicating the CSS from scratch.

In this article, we'll see how WordPress provides native functionality that can be used to generate some native admin tables. We'll have a look at a typical WordPress table and its different components, showing how it is possible to implement them the right way.

Read more
@MrYannoch @toosoonparis Congrats and keep up the good work!17 Sep / 10:26