Pragmatism in the real world

Displaying an RSS feed in WordPress

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 = '

    ';
    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 .= "

  • ";
    $title_string = htmlspecialchars($title_string);
    if ($link_on_title) {
    $output .= ''.$title_string.'';
    }
    $output .= '
    '.$title_string.'

    ';

    if ($date && $display_date) {
    $dateString = date($date_format, strtotime($date));
    if ($link_on_date) {
    $dateString = ''.$dateString.'';
    }
    $output .= '

    '.$dateString.'

    ';
    }
    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 .= '

    '.$summary_string.'

    ';
    }
    $output .= "

  • ";
    }
    $output .= "

";
}

return $output;
}

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

2 thoughts on “Displaying an RSS feed in WordPress

  1. Is there away to hook into this with out the include_once(ABSPATH . WPINC . '/rss.php');

    something like?
    add_filter('rss','akrabat_simple_rss')

    haven't looked yet.

Comments are closed.