Display RSS Feeds with WordPress  Shortcodes

RSS Newspaper on a Bench IconWordPress has built-in the SimplePie RSS parsing engine, making it very easy to display the content of an RSS feed in your theme.

The fetch_feed() function provides access to SimplePie in WordPress, but it’s not that re-usable. I frequently used this handy function for parsing RSS feeds within WordPress themes (or plugins).

A Re-usable RSS Parsing function for WordPress

Place this function in your functions.php file.

/*
*   Re-usable RSS feed reader with shortcode
*/
if ( !function_exists('base_rss_feed') ) {
    function base_rss_feed($size = 5, $feed = 'https://wordpress.org/news/feed/', $date = false, $cache_time = 1800)
    {
        // Include SimplePie RSS parsing engine
        include_once ABSPATH . WPINC . '/feed.php';

        // Set the cache time for SimplePie
        add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', "return $cache_time;" ) );

        // Build the SimplePie object
        $rss = fetch_feed($feed);

        // Check for errors in the RSS XML
        if ( !is_wp_error( $rss ) ) {

            // Set a limit for the number of items to parse
            $maxitems = $rss->get_item_quantity($size);
            $rss_items = $rss->get_items(0, $maxitems);

            // Store the total number of items found in the feed
            $i = 0;
            $total_entries = count($rss_items);

            // Output HTML
            $html = "<ul class='feedlist'>";
            foreach ($rss_items as $item) {
                $i++;

                // Add a class of "last" to the last item in the list
                if( $total_entries == $i ) {
                    $last = " class='last'";
                } else {
                    $last = "";
                }

                // Store the data we need from the feed
                $title = $item->get_title();
                $link = $item->get_permalink();
                $desc = $item->get_description();
                $date_posted = $item->get_date('F j, Y');

                // Output
                $html .= "<li id='post-$i'$last>";
                $html .= "<h3>$title</h3>";
                if( $date == true ) $html .= "<span class='date'>$date_posted</span>";
                $html .= "<div class='rss-entry'>$desc</div>";
                $html .= "</li>";
            }
            $html .= "</ul>";

        } else {

            $html = "An error occurred while parsing your RSS feed. Check that it's a valid XML file.";

        }

        return $html;
    }
}
/** Define [rss] shortcode */
if( function_exists('base_rss_feed') && !function_exists('base_rss_shortcode') ) {
    function base_rss_shortcode($atts) {
        extract(shortcode_atts(array(
            'size' => '10',
            'feed' => 'https://wordpress.org/news/feed/',
            'date' => false,
        ), $atts));

        $content = base_rss_feed($size, $feed, $date);
        return $content;
    }
    add_shortcode("rss", "base_rss_shortcode");
}

Usage

Once you’ve added the functions to your functions.php file, you can use the following in your theme wherever you would like to output a list of RSS entries as an unordered list.

Parameters & Settings

  1. $size — How many entries you want to parse and display. Default value is 5.
  2. $feed — The location of the RSS XML file. Default value is https://wordpress.org/news/feed/.
  3. $date — Display the date for each RSS entry. Default value is false.
  4. $cache_timeOptional. The number of seconds between each time WordPress checks for new items in the feed. Default is 1800, or 30 minutes.

As a Function in a Theme

if( function_exists('base_rss_feed') ) echo base_rss_feed(10, 'https://wordpress.org/news/feed/', true);

As a Shortcode in the WYSIWYG Editor

[rss size="10" feed="https://wordpress.org/news/feed/" date="true"]

The end result of this would look something like:

The HTML Output

<ul class="feedlist">
    <li id="post-1">
        <h3><a href="https://wordpress.org/news/2011/02/threeone/">WordPress 3.1, lots of fun</a></h3>
        <span class="date">February 23, 2011</span>
        <div class="rss-entry">
            The long-awaited fourteenth release of WordPress is now available. WordPress 3.1 “Reinhardt” is named in honor of the jazz guitarist Django Reinhardt. Version 3.1 is available for download, or you can update from within your dashboard. This release features a lightning fast redesigned linking workflow which makes it easy to link to your existing posts and pages, an [...]
        </div>
    </li>
    <li id="post-2">
        <h3><a href="https://wordpress.org/news/2011/02/wordpress-3-0-5/">WordPress 3.0.5 (and 3.1 Release Candidate 4)</a></h3>
        <span class="date">February 7, 2011</span>
        <div class="rss-entry">
            WordPress 3.0.5 is now available and is a security hardening update for all previous WordPress versions. This security release is required if you have any untrusted user accounts, but it also comes with important security enhancements and hardening. All WordPress users are strongly encouraged to update. Three point oh point five Enhances security Three point [...]
        </div>
    </li>
    <!-- Etc... -->
</ul>

Give it some style!

/** RSS Feedlist */
.feedlist {
    list-style:none;
    padding:0;
    margin:0 0 1.6em;
}
.feedlist li {
    padding:0 0 10px;
    border-bottom:1px solid #efefef;
    margin:0 0 10px;
}
.feedlist li:last-child,
.feedlist li.last {
    margin:0;
    border:none;
    padding:0;
}
.feedlist h3 {
    margin:0;
}
.feedlist .date {
    font-size:11px;
    color:#999;
    display:block;
}

As a bonus, the last LI in the list has a class of last. I’ve found that this little bonus is handy for many CSS circumstances until we get full browser support for the :last-child selector.

Resources