Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Displaying an RSS feed in WordPress

DZone's Guide to

Displaying an RSS feed in WordPress

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

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!

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}