Over a million developers have joined DZone.
Platinum Partner

Displaying an RSS feed in WordPress

· Web Dev Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications.Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

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!

The Web Dev Zone is brought to you by Stormpath—offering a complete, pre-built User Management API for building web and mobile applications, and APIs. Download our new whitepaper: "Build Versus Buy: Customer Identity Management for Web and Mobile Applications".


Published at DZone with permission of Rob Allen , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}