WordPress
Article

Quick Tip: Connect WordPress RSS Feeds to Social Networks

By Ian Chandler

Displaying your social media network activity on your website can have tremendous advantages. It shows your readers that you’re an active participant in your niche and helps you establish yourself as a thought leader. The best part is that displaying your activity is fast and easy in WordPress.

WordPress Feeds

To show your social media network (SMN) activity, you’ll parse your account’s RSS feed. The SimplePie and FeedCache functionalities, which are built into WordPress, allow you to use a simple function called fetch_feed. The WordPress Codex has a handy snippet of code available for you, and in this article, we’ll go through the process of finding and parsing your feeds, step by step.

Step 1: Find Your Social Network Feed

First, you’ll have to find your SMN’s specific RSS feed. We’ll go over three popular SMNs: Facebook, Twitter, and Reddit.

For Facebook: Follow these steps, using Zapier to grab the RSS feed from any Facebook page.

For Twitter:TwitRSS.me will easily retrive the feed.

For Reddit: Reddit provides a direct link to your account’s RSS feed. Simply insert your username into this url: https://www.reddit.com/user/YourUsernameHere/.rss

Step 2: Parse Your Feed and Display It on Your Site

Now you’ll use the fetch_feed function to retrieve the RSS feed and display it wherever you choose.

Here’s the code that WordPress.org graciously provides:

<h2><?php _e( 'Recent news from Some-Other Blog:', 'my-text-domain' ); ?></h2>

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'http://example.com/rss/feed/goes/here' );

$maxitems = 0;

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 5 ); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>

<ul>
    <?php if ( $maxitems == 0 ) : ?>
        <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
    <?php else : ?>
        <?php // Loop through each feed item and display each item as a hyperlink. ?>
        <?php foreach ( $rss_items as $item ) : ?>
            <li>
                <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                    <?php echo esc_html( $item->get_title() ); ?>
                </a>
            </li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>

This code generates a list of 5 links, where each link leads to your recent activity. So if you just posted five threads on Reddit, this code will link to those threads.

Copy and paste this code in the sidebar.php file. Make sure to change “example.com” in the above to your RSS feed link.

Now we’ll implement the code.

For the sake of example, let’s say you want to display the feed in your sidebar. In your Dashboard, navigate to Appearance and then Editor. You’ll see a list of .php files; click on sidebar.php. Paste your code into this file and click Update File. You’ll now see your recent SMN activity in your sidebar.

Conclusion

This is a simple, flexible method for displaying your activity on social media networks. You can also modify the code to your liking and use it with other .php files. Even though some may call RSS out of date, it’s still a powerful method that allows you to keep your readers updated with your activity.

  • MyBananaPie

    Great article :D
    Thanks Ian

    • Ian Chandler

      Glad you enjoyed it!

  • http://www.cygnet-infotech.com/ Hemang Rindani

    Insightful! For some businesses it is a must to showcase their social updates on enterprise website and adding a RSS feed section will be the best way to about this.

    I would further like to know that where is the best place to display RSS feeds on a website.

  • http://dzshowstv.com/ dzs-tv

    Great article very usefull thanks for sharing

  • Tyrohn White

    Aritcle is unique but it would have been more useful if some theoretical content was added in it. Earlier i was reading about the same subject on a blog of TemplateToaster they had both theoretical as well as technical content starting from basic to expertise programming.

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Instant Website Review

Use Woorank to analyze and optimize your website to improve your website to improve your ranking!

Run a review to see how your site can improve across 70+ metrics!

Get the latest in WordPress, once a week, for free.