Wordpress Sidebar RSS feed tutorial
Sometimes we want to add some RSS feed into our blog or website from somewhere, the good idea is to add RSS from our another blog to share traffic between blogs, maybe you have some forum and you want to add blog’s RSS feed into it.
The best solution is PHP XML functions to do it on any blog or CMS PHP written platphorm.
To add RSS feed from somewhere open your wordpress sidebar.php, read through code comments to understand where should you put the RSS reading code and output code :
Enter this code before <div id=”sidebar”> html code. Take a look at the code. To change RSS feed url , you need to edit only one variable. $uFile = “http://glurt.com/feed”; Find these line below and change the url to your RSS feed url.
<?
class xItem {
var $xTitle;
var $xLink;
var $xDescription;
}// general vars
$sTitle = “”;
$sLink = “”;
$sDescription = “”;
$arItems = array();
$itemCount = 0;// ********* Start User-Defined Vars ************
// rss url goes here
$uFile = “http://glurt.com/feed”;
// descriptions (true or false) goes here
$bDesc = true;
// font goes here
$uFont = “Verdana, Arial, Helvetica, sans-serif”;
$uFontSize = “2″;
// ********* End User-Defined Vars **************function startElement($parser, $name, $attrs) {
global $curTag;$curTag .= “^$name”;
}
function endElement($parser, $name) {
global $curTag;$caret_pos = strrpos($curTag,’^');
$curTag = substr($curTag,0,$caret_pos);
}
function characterData($parser, $data) { global $curTag; // get the Channel information first
global $sTitle, $sLink, $sDescription;
$titleKey = “^RSS^CHANNEL^TITLE”;
$linkKey = “^RSS^CHANNEL^LINK”;
$descKey = “^RSS^CHANNEL^DESCRIPTION”;
if ($curTag == $titleKey) {
$sTitle = $data;
}
elseif ($curTag == $linkKey) {
$sLink = $data;
}
elseif ($curTag == $descKey) {
$sDescription = $data;
}// now get the items
global $arItems, $itemCount;
$itemTitleKey = “^RSS^CHANNEL^ITEM^TITLE”;
$itemLinkKey = “^RSS^CHANNEL^ITEM^LINK”;
$itemDescKey = “^RSS^CHANNEL^ITEM^DESCRIPTION”;if ($curTag == $itemTitleKey) {
// make new xItem
$arItems[$itemCount] = new xItem();// set new item object’s properties
$arItems[$itemCount]->xTitle = $data;
}
elseif ($curTag == $itemLinkKey) {
$arItems[$itemCount]->xLink = $data;
}
elseif ($curTag == $itemDescKey) {
$arItems[$itemCount]->xDescription = $data;
// increment item counter
$itemCount++;
}
}// main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, “startElement”, “endElement”);
xml_set_character_data_handler($xml_parser, “characterData”);
if (!($fp = fopen($uFile,”r”))) {
die (”could not open RSS for input”);
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf(”XML error: %s at line %d”, xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);// write out the items in sidebar
?>
The last part is to add output code into our sidebar, find <div id=”sidebar”> html code and enter the code below between <div id=”sidebar”> and </div> tag on any place inside your sidebar.
<?php
for ($i=0;$i<4;$i++) {
$txItem = $arItems[$i];
?><h3><a href = “<?=$txItem->xLink ?>”><?php echo(trim($txItem->xTitle)); ?></h3></a>
<p><?php
$texter = trim($txItem->xDescription);
if(strlen($texter ) > 120) {
$texter = substr($texter , 0, 120);
}
echo ”.$texter.’<br><a href = “‘.$txItem->xLink.’”> Read more </a>’;
?>
</p><?
}
?>
Now we have pretty RSS code shown from somewhere on our wordpress sidebar .

Rich
This is one of the reasons I luv WordPress.There are so many great tutorials about how to hack it.
July 5th, 2009 at 4:49 pm