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

Getting started with React.js illustration

As a lead developer, I watch the web development market for solid trends (not the hype ones), and see if they could be a tangible addition to our workflow. Like Angular a few years ago, React has gained enough momentum to catch my attention. I decided to try it out in a side project to get a better understanding of how it works. Here are a few things I've noted from our first encounter. This is a beginner's point of view, not a React expert's one, so if you see any mistakes, don't hesitate to let me know, I'll update the article accordingly.

Read more
Form Cloning jQuery Plugin illustration

Building dynamic forms is a task that a lot of web developers will have to do at some point. By dynamic I mean forms that change based on what the user inputs.

A basic example of dynamic forms, are those that allow the user to add the same group of information several times. For example, an attendance form, where you can add several persons, a booking form where you can add several tickets, a membership with several users, a media page with several pictures or videos and so on, the possibilities are countless.

In order to facilitate the development of such forms, I created a little plugin, that allows you to dynamically clone a specific set of information that will be repeated. So if you ever searched a way of cloning forms, fieldsets or groups of input, this could really be helpful.

Read more
How to turn any jQuery plugin into a WordPress one illustration

WordPress and jQuery are both very famous for their plugins. In the case of jQuery, plugins allow developers to extend the library's capacities in order to create beautiful effects. In WordPress, they allow anyone to benefit from the work of others, as they come under the form of a module that you can easily activate from within the administration interface, and start using right away.

Creating one of those modules is not that difficult, but if you are not familiar with their syntax, it could end up being a headache. There are many jQuery plugins that have been transferred into a WordPress one already, but what can you do when it is not the case? How do you do when you have found the perfect jQuery plugin for your latest site, but it is not yet available as a WordPress plugin?

In this tutorial, we'll see how you can easily create a WordPress plugin from a jQuery one. We'll review what a jQuery, or a WordPress plugin is actually made of, so we'll find the elements they both have in common. From there, we'll show how it leads us to perform some simple operations to allow us to benefit from our jQuery plugins in a WordPress template.

Read more
@ambroisemaupate @orange Moi j'ai pris une 503 toute à l'heure. #irony25 Mar / 16:20