Rob Allen has been programming with PHP since 1999 and is a member of the PHP community. He is the lead author of Zend Framework in Action and is a contributor to Zend Framework, developing the Zend_Config component. Rob holds a Masters degree in Electronic Engineering from the University of Birmingham in the UK and is the Technical Director of Big Room Internet in the UK, focussing on project management and the company’s future technologies. Rob is a DZone MVB and is not an employee of DZone and has posted 31 posts at DZone. You can read more from them at their website. View Full User Profile

Displaying an RSS feed in WordPress

01.03.2011
| 3472 views |
  • submit to reddit

My wife decided that she wanted to display a list of her latest AudioBoos in the sidebar of her blog. She looked at the AudioBoo JavaScript widget but decided it wasn't subtle enough and so she enlisted me to solve her problem.

It turns out that AudioBoo has an RSS feed, so a simple plugin was required. I had a quick look on the extension site, but most are now "widgets" which her theme isn't set up for or didn't provide an unsigned list. Hence, I whipped up a small extension for her.

It turns out that WordPress ships with MagpieRSS baked in, so the work to get the feed is trivial:

include_once(ABSPATH . WPINC . '/rss.php');
$messages = fetch_rss($url);

The rest of the work is simply formatting the output. The key requirement that she had was that it should provide an unsigned list with title, date and optionally the summary. Along with providing some customisation for her, this is what I came up with:

function akrabat_simple_rss($options = array()) 
{
$defaults = array(
'url' => '',
'number_of_items' => 5,
'display_date' => true,
'date_format' => 'd M Y at H:i',
'display_summary' => true,
'number_of_summary_chars' => 100,
'link_on_title' => true,
'link_on_date' => false,
'css_class' => 'akrabat-simple-rss',
);

extract (array_merge($defaults, $options));

$output = '';
if (!empty($url)) {
include_once(ABSPATH . WPINC . '/rss.php');
$messages = fetch_rss($url);
if(count($messages->items) == 0){
return '';
}

if($number_of_items > count($messages->items)) {
$number_of_items = count($messages->items);
}

$output = '<ul class="'.$css_class.'">';
for($i = 0; $i < $number_of_items; $i++){
$message = $messages->items[$i];

$link = $message['link'];
$title = $message['title'];
$date = null;
if (isset($message['published'])) {
$date = $message['published'];
}
if (!$date && isset($message['pubdate'])) {
$date = $message['pubdate'];
}
$summary = null;
if (isset($message['summary'])) {
$summary = $message['summary'];
}
if (!$summary && isset($message['description'])) {
$summary = $message['description'];
}

$output .= "<li>";
$title_string = htmlspecialchars($title_string);
if ($link_on_title) {
$output .= '<a href="'.$link.'">'.$title_string.'</a>';
}
$output .= '<div class="akrabat-rss-title">'.$title_string.'</div>';

if ($date && $display_date) {
$dateString = date($date_format, strtotime($date));
if ($link_on_date) {
$dateString = '<a href="'.$link.'">'.$dateString.'</a>';
}
$output .= '<div class="date">'.$dateString.'</div>';
}
if ($summary && $display_summary) {
$summary_string = substr($summary, 0, $number_of_summary_chars);
if (count(summary) > $number_of_summary_chars) {
$summary_string = substr(summary_string, 0, -3) . '...';
}
$summary_string = htmlspecialchars($summary_string);
$output .= '<div class="summary">'.$summary_string.'</div>';
}
$output .= "</li>";
}
$output .= "</ul>";
}

return $output;
}

Maybe it's useful to someone else too, and I've documented it somewhere!

References
Published at DZone with permission of Rob Allen, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: