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=" . wp_filter_kses($item['link']) . ">" . wptexturize(wp_specialchars($item['author'])) . "</a></p>";
      }
      echo $before_after;
  }

Related posts

Leave a Reply

2 comments

  1. 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;
    }
    
  2. The default lifetime of a transient cached feed is 12 hours. It can be changed by hooking in to the wp_feed_cache_transient_lifetime filter.

    This will change the feed cache to update every 10 minutes:

    add_filter( 'wp_feed_cache_transient_lifetime', 
                 create_function('$a', 'return 600;') );
    

    Also the code you copied and pasted is pretty outdated. It contains deprecated functions and does not take advantage of the Widgets API. fetch_rss was replaced with fetch_feed. wp_specialchars() was replaced with esc_html() -> see WP Hackers Discussion.

    The Widgets API is very well documented and with a few modifications your code could take advantage of the new features.