
Prior to version 2.8 of WordPress (and since version 1.5), if you wanted an RSS feed on your WP website you would use wp_rss() … and it works fine. However, support for the very fast and reliable SimplePie
library was added in version 2.8 – and it’s a far better option. This article will show you how to added a feed to your WordPress website (with only basic options) and cache the result with the WordPress transient API.
What is an RSS feed? RSS (Rich Site Summary) is a format for delivering regularly changing web content. Many news-related sites, blogs, and other online publishers syndicate their content as an RSS Feed to whoever wants it. All WordPress websites usually have an RSS feed available by default.
The Result
Using the shortcode of [rss feed="http://www.beliefmedia.com/feed" size="3"]
we’ll render the three latest posts from this website. The result is as follows:
FAA Aircraft Registration API - 19th Apr 2018
Of all the services we make available, the FAA aircraft registration API might be the oldest. It was set up as as part of an aircraft search engine at FlightPlanning.com ...
SEO Snapshot of Sydney Mortgage Broker Websites - 16th Apr 2018
When it comes to Mortgage Brokers, there really are two kinds: those that consistently write in excess of 100 million dollars a year (and their business is growing), and those ...
History API - 13th Apr 2018
For a long time we’ve maintained a ‘This Day in History‘ database for use by clients and partners. The purpose of this article is to provide a cursory introduction to ...
The last three results (without description) from our neglected FLIGHT website may be returned with the shortcode of [rss feed="http://www.flight.org/feed" size="3" heading="h5" excerpt="0"]
. The result:
Cold Temperature Altimetry Corrections - 5th Nov 2016
The Boeing 777 Rejected Landing - 10th Aug 2016
Low Missed Approach Altitude Restrictions - 12th Apr 2016
Using our textbox shortcode we can wrap a feed (this time our YouTube RSS feed) with the following result:
Mortgage Broker Telephone Scam - 23rd Mar 2018
BeliefMedia Instagram Application (Approval Request) - 23rd Jan 2018
Facebook Carousel Example - 11th Jan 2018
Shortcode Attributes
The following shortcode attributes are available:
feed
http://www.beliefmedia.com/feed
.size
excerpt
excerpt="30"
(where 30 is the number of words to return). To disable the excerpt, use excerpt="0"
.more
more
text is the trailing dots or other characters of your choosing.date
date="1"
(default). To disable, use date="0"
.datecss
font-weight: normal
by default.dateformat
dateformat="jS M Y"
. All available date options are listed in the PHP manual 
heading
h4
tags by default.list
list="0"
.target
cache

WordPress Shortcode
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
/* A Better Way to Add RSS Feeds to Your WordPress Blog (Using Shortcode) http://www.beliefmedia.com/embed-rss-wordpress */ function beliefmedia_simplepie_rss($atts) { $atts = shortcode_atts(array( 'feed' => 'http://www.beliefmedia.com/feed/', 'size' => '5', 'excerpt' => 30, 'more' => ' ...', 'date' => 1, 'datecss' => 'font-weight: normal', 'dateformat' => 'jS M Y', 'heading' => 'h4', 'list' => 1, 'target' => '_blank', /* _blank, _self etc.. */ 'cache' => 3600 * 6 ), $atts); $transient = 'bmrss_' . md5(serialize($atts)); $cachedposts = get_transient($transient); if ($cachedposts !== false) { return $cachedposts; } else { /* SimplePie RSS parsing engine */ include_once ABSPATH . WPINC . '/feed.php'; /* Build the SimplePie object */ $rss = fetch_feed($atts['feed']); /* Check for errors in the RSS XML */ if (!is_wp_error($rss)) { $maxitems = $rss->get_item_quantity($atts['size']); $rss_items = $rss->get_items(0, $maxitems); $total_entries = count($rss_items); $html .= '<p>'; if ($list) $html .= "<ul class='feedlist'>"; $i = 1; foreach ($rss_items as $item) { $title = $item->get_title(); $title = $title . ' '; $link = $item->get_permalink(); $desc = $item->get_description(); $date_posted = $item->get_date($atts['dateformat']); if ($atts['excerpt'] !== false) { /* http://codex.wordpress.org/Function_Reference/wp_kses */ $desc = wp_kses(trim($desc), array()); $desc = strip_tags(apply_filters('the_excerpt', $desc)); $desc = wp_trim_words($desc, $atts['excerpt'], $atts['more']); $desc = trim(preg_replace('!\s+!', ' ', $desc)); } /* Add item to result */ if ($atts['list']) { /* Output */ $html .= '<li id="post-' . $i . '">'; $html .= '<' . $atts['heading'] . '><a href="' . $link . '" target="' . $atts['target'] . '" title="' . $title . '">' . $title . '</a>'; if ($atts['date']) $html .= ' - <span class="date" style="' . $atts['datecss'] . ';">' . $date_posted . ' </span>'; $html .= '</' . $atts['heading'] . '>'; if ($atts['excerpt']) $html .= '<p>' . $desc . '</p>'; $html .= '</li>'; } else { $html .= '<' . $atts['heading'] . '><a href="' . $link . '" target="' . $atts['target'] . '" title="' . $title . '">' . $title . '</a>'; if ($atts['date']) $html .= ' - <span class="date" style="' . $atts['datecss'] . ';">' . $date_posted . ' </span>'; $html .= '</' . $atts['heading'] . '>'; if ($atts['excerpt']) $html .= '<p>' . $desc . '</p>'; } $i++; } $html = ($atts['list']) ? '<p><ul class="feedlist">' . $html . '</ul></p>' : '<p>' . $html . '</p>'; } else { $html = '<p>Error. Visit the feed <a href="' . $feed . '">here</a>.</p>'; } set_transient($transient, $html, $atts['cache']); return $html; } } add_shortcode('rss', 'beliefmedia_simplepie_rss'); |
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
Considerations
- This function is ported from the now retired Internoetics with only minor modifications. It could do with an update!
Download
Shortt URL for this post: http://shor.tt/17N9