How to set the cache for the built-in SimplePie feed parser?

I’m using the built-in SimplePie, AKA fetch_feed(), to retrieve a feed, and I want to be able to adjust the cache time from an admin menu. SimplePie itself is well documented, but not so much the WordPress implementation of it.

Any thoughts on the best way to set the cache duration?

Related posts

Leave a Reply

2 comments

  1. Cache duration value (defaults to 43200 seconds) is set when feed object is generated and passed through wp_feed_cache_transient_lifetime filter with additional argument being feed URL.

    This allows to conveniently filter it both globally and for specific feeds.

    See fetch_feed() source for this and other hooks you can use to modify its behavior.

  2. To elaborate on Rarst’s solutuion. I’ve been using the following to great success:

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

    This sets the feed cache at one hour and should be placed immediately after the feed.php include.