How to Generate RSS Feed for Custom Component in Joomla?

Here, I’ve compiled a guide on how to generate RSS Feed for Custom Component in Joomla using 5 simple steps. I hope this guide will help anyone who wishes to generate RSS Feed in Joomla website.

To generate RSS Feed for custom component in Joomla, follow the below steps:

1. Create a new view and model in the name “rss”

Read: How To Add A New Module Position In Joomla Template?

view.html.php:

<?php
// No direct access to this file
defined( ‘_JEXEC’ ) or die( ‘Restricted access’ );
jimport( ‘joomla.application.component.view’);class customcomponentViewrss extends JViewLegacy
{function display($cachable = false, $urlparams = false)
{
$model =& $this->getModel();
$detail = $model->getrecords();
}}
?>

views/rss/tmpl/default.php:

<?php
// No direct access to this file
defined(‘_JEXEC’) or die(‘Restricted access’);
?>

2. Your model file should retrieve database values of your component
3. Then generate XML using the retrieved values.
4. Your XML should be in the below format.

header(“Cache-Control: no-cache, must-revalidate”);
header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
header(“content-type: text/xml”);
echo ‘<?xml version=”1.0″ encoding=”utf-8″?>’;
echo ‘<rss xmlns:content=”http://purl.org/rss/1.0/modules/content/” version=”2.0″>’;
echo ‘<title>’.$YOUR_SITE_TITLE_MENTIONED_IN_GLOBAL_CONFIGURATION.’</title>’;
echo ‘<link>’.$YOUR_DOMAIN_BASE_URL.’</link>’;
foreach ($db_value as $value) {
echo ‘<item>’;
echo ‘<id>’ . $value->id . ‘</id>’;
echo ‘<image>’ . $value->image . ‘</image>’;
echo ‘<hits>’ . $value->hits . ‘</hits>’;
echo ‘<createdDate>’ . $value->date . ‘</createdDate>’;
echo ‘<title>’;
echo ‘<![CDATA[' . $value->title . ']]>’;
echo ‘</title>’;
echo ‘<description>’;
echo ‘<![CDATA[' . $value->description . ']]>’;
echo ‘</description>’;
echo ‘<tags>’;
echo ‘<![CDATA[' . $value->tags . ']]>’;
echo ‘</tags>’;
echo ‘<link>’ . $EVERY_PAGE_URL_OF_YOUR_ITEM . ‘</link>’;
echo ‘<generator>Feed_Name</generator>’;
echo ‘<docs>http://blogs.law.harvard.edu/tech/rss</docs>’;
echo ‘</item>’;
}
echo ‘</rss>’;

Example code to generate RSS XML in Model file: (models/rss.php)

<?php
// No direct access to this file
defined(‘_JEXEC’) or die(‘Restricted access’);
// import joomla model library
jimport(‘joomla.application.component.model’);
class Modelcustomcomponentrss extends JModelList{function getrecords() {
$db = JFactory::getDBO();
$query = “SELECT * FROM #__YOUR_TABLE_NAME ORDER BY id DESC”;
$db->setQuery($query);
$db_value = $db->loadObjectList();
$this->showxml($db_value);
}
function showxml($db_value) {
ob_clean();
header(“Cache-Control: no-cache, must-revalidate”);
header(“Expires: Mon, 26 Jul 1997 05:00:00 GMT”);
header(“content-type: text/xml”);
echo ‘<?xml version=”1.0″ encoding=”utf-8″?>’;
echo ‘<rss xmlns:content=”http://purl.org/rss/1.0/modules/content/” version=”2.0″>’;
echo ‘<title>’.$YOUR_SITE_TITLE_MENTIONED_IN_GLOBAL_CONFIGURATION.’</title>’;
echo ‘<link>’.$YOUR_DOMAIN_BASE_URL.’</link>’;
foreach ($db_value as $value) {
echo ‘<item>’;
echo ‘<id>’ . $value->id . ‘</id>’;
echo ‘<image>’ . $value->image . ‘</image>’;
echo ‘<hits>’ . $value->hits . ‘</hits>’;
echo ‘<createdDate>’ . $value->date . ‘</createdDate>’;
echo ‘<title>’;
echo ‘<![CDATA[' . $value->title . ']]>’;
echo ‘</title>’;
echo ‘<description>’;
echo ‘<![CDATA[' . $value->description . ']]>’;
echo ‘</description>’;
echo ‘<tags>’;
echo ‘<![CDATA[' . $value->tags . ']]>’;
echo ‘</tags>’;
echo ‘<link>’ . $EVERY_PAGE_URL_OF_YOUR_ITEM . ‘</link>’;
echo ‘<generator>Feed_Name</generator>’;
echo ‘<docs>http://blogs.law.harvard.edu/tech/rss</docs>’;
echo ‘</item>’;
}
echo ‘</rss>’;
exit();
}
}
?>

5. Now place the following code where ever you want to display rssfeed link in your site.

<a href=”<?php echo “YOUR_SITE_URL/index.php?option=com_customcomponent&view=rss”; ?>”
id=”rssfeed” target=”_blank” >RSS Feed</a>

By now, you’d have developed some ideas on how to generate RSS Feed using Custom Component in Joomla website.

Realated Posts

2 thoughts on “How to Generate RSS Feed for Custom Component in Joomla?

  1. Swapnil Patil

    Hello there,

    I am sorry to ask you basic question but i am pretty new to the Joomla development and would like to develop a custom RSS feed XML from all the Joomla Article.

    Could you please suggested the starting point for your above solution? I well understood the basic PHP and several other server side technologies. Appreciate your quick response. Thanks in Advance

    Thanks and regards,
    Swapnil

  2. MSaudi

    Can we do this with modules ?

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 lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">