Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
  }
share|improve this question

2 Answers 2

up vote 2 down vote accepted

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;
}
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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