0

I'm trying to see if there's a way I can make it where the RSS widget can pull in a different feed (one of let's say ten I pick) and displays it in the widget each time the page is refreshed?

It's kind of like this post (Different rss feeds in a single dashboard widget) except this person is trying to display several feeds at once and I want to rotate it from one feed to another each time you refresh the page or go to a different page. So it only displays one feed, but it's different every time.

0

Here's one way to achieve this:

/**
 * Display a random feed in the RSS Widget
 * Activated by the wpse_random_url string in the feed url
 *
 * @see http://wordpress.stackexchange.com/a/187599/26350
 */

! is_admin() && add_filter( 'wp_feed_options', function( $feed, $url )
{
    // Modify this list of feeds to your needs:
    $urls = [
        'http://stackoverflow.com/feeds/', 
        'http://wordpress.stackexchange.com/feeds',
        'http://unix.stackexchange.com/feeds'
    ];

    // Select a random feed from the above list:            
    if(    class_exists( '\SimplePie' )
        && $feed instanceof \SimplePie 
        && false !== strpos( $url, 'wpse_random_url' ) 
        && method_exists( $feed, 'set_feed_url' ) 
    )
            $feed->set_feed_url( $urls[ array_rand( $urls ) ] );

    return $feed;
}, 10, 2 );

where our feed url must contain the wpse_random_url string.

We could for example use our current site's feed, with the random_url GET parameter:

http://example.tld/feed/?wpse_random_url

and add that to the url field of the RSS widget:

Random feed GET parameter

The next step to improve this mini plugin, would be to introduce a way to add the list of feeds, directly from the backend instead of having to modify it within the code itself.

  • Would you place this code inside of the theme's functions.php file? Or just make a new plugin? – Adam Bell May 7 '15 at 20:08
  • Never mind. I placed it within the comments and it worked. Thanks for helping out! – Adam Bell May 7 '15 at 20:33
  • You're welcome @AdamBell , I'm glad to hear it worked for you. But I think it would be more flexible to introduce a shortcode, for example: [wpse_random_rss urls="#1|#2|#3|...etc..."] to display a random feed in a Text widget – birgire May 9 '15 at 21:56
  • 1
    Thanks, but this actually works well enough for me. – Adam Bell May 10 '15 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.