WordPress.org

Ready to get started?Download WordPress

Forums

Fetch_Feed cache time (2 posts)

  1. Dave Navarro, Jr.
    Member
    Posted 1 year ago #

    Please update Fetch_Feed to support an optional parameter for the cache time value.

    I know that I can change the global time using the wp_feed_cache_transient_lifetime filter, but I need the ability to set individual cache times. Some feeds I want to update more quickly than others.

    In my testing, I modified fetch_feed with the following:

    function fetch_feed( $url, $cache_time = NULL )
     ...
    	if ($cache_time == NULL) {
    	  $cache_time = apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $url );
    	}
    ...
    	$feed->set_cache_duration( $cache_time );

    which doesn't break existing calls, but allows me to set individual cache times for feeds.

  2. You can always remove the filter directly after the call to fetch_feed(). Probably better than hacking core is to create a wrapper function that allows you set the cache time individually for each feed.

    function dn_fetch_feed( $url, $cache_time = false ) {
         if ( $cache_time ) {
             add_filter( 'wp_feed_cache_transient_lifetime', function() {
                 return $cache_time;
             });
             $feed = fetch_feed( $url );
             remove_all_filters( 'wp_feed_cache_transient_lifetime' );
    
            return $feed;
        }
    
        return fetch_feed( $url );
    
    }

Topic Closed

This topic has been closed to new replies.

About this Topic