This blog is NOFOLLOW Free!
  • INCEPTION THEME

    Clean, contrasting WordPress theme built on the foundation of Whiskey Air.
  • WHISKEY AIR THEME

    Clean developer theme, suited for heavy modifications.
  • X5 TURBO THEME

    3 columns Turbo theme, now reloaded!
  • CLEAR APPLE THEME

    Almost magazine theme, without the hassle of administration.
How to Add a RSS Feed to a Static Site

Static RSS FeedI’ll be straying a bit from WordPress to static PHP web site owners.

Sometimes you need an RSS feed to provide your users if you have a static news site powered by PHP/MySQL. Sometimes you just need it in order to promote your site, or even to automatically post to Twitter or Facebook.

What do you do if you need an RSS feed but don’t want to make the big step towards WordPress? Well, you write your own script:

<?php echo'<?xml version="1.0" encoding="UTF-8"?>'."\n";?>
<rss version="2.0"
 xmlns:content="http://purl.org/rss/1.0/modules/content/"
 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:atom="http://www.w3.org/2005/Atom"
>

<channel>
 <title>My New RSS Feed</title>
 <link>http://www.mydomain.com/</link>
 <atom:link href="http://www.mydomain.com/rss2.php" rel="self" type="application/rss+xml" />
 <description>News from my new RSS feed</description>
 <language>en</language>

 <?php
 include('/how_to_add_a_rss_feed_to_a_static_site/include/database.html');
 include('/how_to_add_a_rss_feed_to_a_static_site/includes/config_settings.html');

 $result = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
 while($row = mysql_fetch_array($result)) {
 echo '
 <item>
 <title>'.$row['title'].'</title>
 <guid isPermaLink="true">http://www.mydomain.com/news.php?id='.$row['id'].'</guid>
 <link>http://www.mydomain.com/news.php?id='.$row['id'].'</link>
 <description>'.str_replace("&","and",trim(substr(strip_tags($row['content']),0,225))).'... &lt;a href="http://www.mydomain.com/news.php?id='.$row['id'].'"&gt;Read More!&lt;/a&gt;</description>
 <pubDate>'.date('D, d M y H:i:s O', strtotime($row['thedate'])).'</pubDate>
 </item>';
 }
 ?>
</channel>
</rss>

Let’s break it down. The first section is pretty self-explanatory, as you need to include a special RSS 2.0 header, and RSS feed details, such as title, link, description and language.

You should then include your database connection files (you may have it named db.php, config.php or connect.php) in order to execute the query. Notice how I have my own news table which I extract data from.

Copy the code above in a rss2.php file and call it in your browser. Or use it to ping a RSS feed service.

That’s it.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>