Decrease RSS cache time in plugin?

I’d like to decrease the default RSS cache time for a widget plugin i’ve made that outputs RSS feeds. I’m no php expert, I just copy and paste and understand little bits. I’m using this code to output the RSS

  function widget($args, $instance) {
  // outputs the content of the widget
      extract( $args );
      $title = apply_filters('widget_title', $instance['title']);        
      $tip_language = apply_filters('widget_title', $instance['tip_language']);        
      echo $before_widget;
      echo "<h3 class=\"widgettitle\">" . $title . "</h3>";
      include_once(ABSPATH . WPINC . '/rss.php');
      $rss_feed = "http://mysite.org/feed";
      $rss = fetch_rss($rss_feed);
      $rss->items = array_slice($rss->items, 0, 1);
      $channel = $rss->channel;

      foreach ($rss->items as $item ) {
          $parsed_url = parse_url(wp_filter_kses($item['link']));
          echo wptexturize($item['description']);
          echo "<p><a href="https://wordpress.stackexchange.com/questions/6569/. wp_filter_kses($item["link']) . ">" . wptexturize(wp_specialchars($item['author'])) . "</a></p>";
      }
      echo $before_after;
  }

2 Answers
2

Why not just use native RSS widget?

Also while Chris_O is absolutely right on filter to use, it is better practice to change cache time for specific feed URL rather than globally:

add_filter( 'wp_feed_cache_transient_lifetime', 'speed_up_feed', 10, 2 );

function speed_up_feed( $interval, $url ) {

    if( 'http://mysite.org/feed' == $url )
        return 3600;

    return $interval;
}

Leave a Comment