RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (5.84%*) • Fixed: 5.39% (5.84%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.19%*) • Investment PI: 5.55% (6.02%*)

A Better Way to Add RSS Feeds to Your WordPress Blog (Using Shortcode)

A Better Way to Add RSS Feeds to Your WordPress Blog (Using Shortcode)

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:

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:

Error. Visit the feed here.

Using our textbox shortcode we can wrap a feed (this time our YouTube RSS feed) with the following result:

Shortcode Attributes

The following shortcode attributes are available:

feed

The RSS feed you would like to consume. For example: http://www.beliefmedia.com/feed.

size

The number of results to return.

excerpt

If you would like to include an excerpt, use excerpt="30" (where 30 is the number of words to return). To disable the excerpt, use excerpt="0".

more

If an excerpt is included and the text is longer than the defined length, the more text is the trailing dots or other characters of your choosing.

date

To include the date (concatenated to the title), use date="1" (default). To disable, use date="0".

datecss

If using a date you may apply your own style to differentiate it from your title. We use font-weight: normal by default.

dateformat

To format the date output, use dateformat="jS M Y". All available date options are listed in the PHP manual .

heading

The heading is the tags wrapped around the post title. We use h4 tags by default.

list

Results are rendered in a list by default. To disable, use list="0".

target

The target is that of the resulting title click. We return the page in a new tab by default.

cache

The cache time should really be set to something longer to 12 hours because this is the default cache value of wp_feed_cache_transient_lifetime . To alter the feed transient time there's a sample filter function in the WP codex.

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
<?php 
2
/*
3
 A Better Way to Add RSS Feeds to Your WordPress Blog (Using Shortcode)
4
 http://www.beliefmedia.com/embed-rss-wordpress
5
*/
6
 
7
function beliefmedia_simplepie_rss($atts) {
8
 
9
  $atts = shortcode_atts(array(
10
    'feed' => 'http://www.beliefmedia.com/feed/',
11
    'size' => '5',
12
    'excerpt' => 30,
13
    'more' => ' ...',
14
    'date' => 1,
15
    'datecss' => 'font-weight: normal',
16
    'dateformat' => 'jS M Y',
17
    'heading' => 'h4',
18
    'list' => 1,
19
    'target' => '_blank', /* _blank, _self etc.. */
20
    'cache' => 3600 * 6
21
  ), $atts);
22
 
23
 $transient = 'bmrss_' . md5(serialize($atts));
24
 $cachedposts = get_transient($transient);
25
 
26
 if ($cachedposts !== false) {
27
   return $cachedposts;
28
 
29
  } else {
30
 
31
  /* SimplePie RSS parsing engine */
32
  include_once ABSPATH . WPINC . '/feed.php';
33
 
34
  /* Build the SimplePie object */
35
  $rss = fetch_feed($atts['feed']);
36
 
37
  /* Check for errors in the RSS XML */
38
  if (!is_wp_error($rss)) {
39
 
40
     $maxitems = $rss->get_item_quantity($atts['size']);
41
     $rss_items = $rss->get_items(0, $maxitems);
42
     $total_entries = count($rss_items);
43
 
44
 $html .= '';
45
 if ($list) $html .= "
46
<ul class='feedlist'>";
47
 
48
        $i = 1;
49
 foreach ($rss_items as $item) {
50
 
51
   $title = $item->get_title();
52
   $title = $title . ' ';
53
   $link = $item->get_permalink();
54
   $desc = $item->get_description();
55
   $date_posted = $item->get_date($atts['dateformat']);
56
 
57
     if ($atts['excerpt'] !== false) {
58
 
59
              /* http://codex.wordpress.org/Function_Reference/wp_kses */
60
              $desc = wp_kses(trim($desc), array());
61
              $desc = strip_tags(apply_filters('the_excerpt', $desc));
62
              $desc = wp_trim_words($desc, $atts['excerpt'], $atts['more']);
63
              $desc = trim(preg_replace('!\s+!', ' ', $desc));
64
 
65
     }
66
 
67
          /* Add item to result */
68
          if ($atts['list']) {
69
 
70
     /* Output */
71
            $html .= '
72
<li id="post-' . $i . '">';
73
            $html .= '<' . $atts['heading'] . '><a href="' . $link . '" target="' . $atts['target'] . '" title="' . $title . '" rel="noopener noreferrer">' . $title . '</a>';
74
            if ($atts['date']) $html .= ' - <span class="date" style="' . $atts['datecss'] . ';">' . $date_posted . ' </span>';
75
            $html .= '</' . $atts['heading'] . '>';
76
            if ($atts['excerpt']) $html .= '' . $desc . '';
77
            $html .= '</li>
78
 
79
';
80
 
81
            } else {
82
 
83
            $html .= '<' . $atts['heading'] . '><a href="' . $link . '" target="' . $atts['target'] . '" title="' . $title . '" rel="noopener noreferrer">' . $title . '</a>';
84
            if ($atts['date']) $html .= ' - <span class="date" style="' . $atts['datecss'] . ';">' . $date_posted . ' </span>';
85
            $html .= '</' . $atts['heading'] . '>';
86
            if ($atts['excerpt']) $html .= '' . $desc . '';
87
 
88
          }
89
 
90
         $i++;
91
        }
92
 
93
        $html = ($atts['list']) ? '<ul class="feedlist">' . $html . '</ul>
94
' : '' . $html . '';
95
 
96
        } else {
97
 
98
        $html = 'Error. Visit the feed <a href="' . $feed . '">here</a>.';
99
   }
100
 
101
  set_transient($transient, $html, $atts['cache']);
102
  return $html;
103
 }
104
}
105
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


Title: Add RSS Feeds to WordPres (WP Shortcode)
Description: A Better Way to Add RSS Feeds to Your WordPress Blog (Using Shortcode).
  Download • Version 0.2, 1.3K, zip, Category: WordPress Shortcodes

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment