Feed Cache in WordPress

WordPress already offers the possibility of own functions to read feeds and use it in your blog. Since version 2.8 of WordPress, a new feature is in use for that. So there are two ways to work with feeds in WordPress, and in both functions, the data is cached. But this is not always wanted, so I show how to take influence on caching of the two functions.

fetch_feed() Since WordPress 2.8

With version 2.8 of WordPress, they add the class SimplePie into the core. SimplePie has always distinguished by a very rapid development and a stable user-friendly class. Before MagpieRSS has been used, which in many cases was not available and the development is rather slow. For some time SimplePie is no longer be maintained by the developer, which worried the developer community. But developers from the WordPress team took care of it and keep the class alive. More information can be find on the blog WP Dev Updates

Now the class is in the core of WordPress and is capable of generating RSS and ATOM feeds. It uses the cache of WordPress and can be controlled via a hook.

But first a small sample to read a feed with the class in WordPress.

get_items( 0, $rss->get_item_quantity(5) );
if ( !$rss_items ) {
    echo 'no items';
} else {
    foreach ( $rss_items as $item ) {
        echo '

' . $item->get_title() . '

'; } } ?>

The small sample reads my feed and returns the last 5 entries.

Essential is the function fetch_feed(). This function has a hook which can make influence on caching – wp_feed_cache_transient_lifetime.
The standard of the cache is set to 12 hours ( 43,200 ), which is not always the favorite setting for users. The value is operated on the method WP_Feed_Cache_Transient(), parameter $lifetime. Therefore, it is advisable to use the following little query, be it in the Plugin or in the functions.php of the theme.

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

In this example, I set the cache to 30 minutes. With the help of Conditional Tags you can control the whole of it so that the cache-hook is only activated when you click on a page (is_page()) or for example you are in the front end (!is_admin()) of the blog. Here you must simply use the opportunities and adapt to your own needs.

A small possibility I would like to point out more, available as of WordPress 2.9 (ticket 11117), to simplify debugging for developers. Thus, the cache is disabled in the environment, if the constant WP_DEBUG is set, which can be important now and then. Setting the constant is usually done in the wp-config.php of the installation, or as alternative in the Plugin.

function do_not_cache_feeds(&$feed) {
	$feed->enable_cache(false);
}

if ( defined('WP_DEBUG') && WP_DEBUG )
	add_action( 'wp_feed_options', 'do_not_cache_feeds' );

fetch_rss()

Another function is fetch_rss(), which was set with WordPress 2.8 on the suspense list. As an alternative, the above function is active. Current is fetch_rss() still available and is also supported. Therefore, for completeness, also the possibility to influence the cache.

Here an example to read feed and output the last 5 entries of the feed

items, 0, 5);
if ( empty($rss_items) ) {
    echo 'no items';
} else {
    foreach ( $rss_items as $item ) {
        echo '

' . $item['title'] . '

'; } } ?>

Here, the function fetch_rss() is used for reading and the cache can be changed via the constants. This must defined either directly in wp-config.php where they will apply globally to all applications, or you need to integrate them into its own functions.

define('MAGPIE_CACHE_ON', 0); // deactivate cache
define('MAGPIE_CACHE_AGE', 60*60) // Cache 1 hour in seconds

Conclusion

Since WordPress decided not to support MagpieRSS any longer in the future, you should only deal with SimplePie and not with the second possibility.


Posted

in

by

Comments

13 responses to “Feed Cache in WordPress”

  1. r-a-y Avatar
    r-a-y

    Does the new SimplePie library in WP 2.8 cache to the DB or to a directory?

    I’d rather RSS feeds cache to a directory.

  2. Kim Woodbridge Avatar

    Thanks for this post! I started using the fetch feed function to display delicious bookmarks feeds with specific tags in the footer of my site. I couldn’t figure out why the feed wasn’t being updated even though I had added new bookmarks with that tag.

    I found your article, added the add filter to my functions file and now the feed is being updated.

  3. Scott Phillips Avatar
    Scott Phillips

    Anyone know if wp_feed_cache_transient_lifetime is global and applies to all feeds, or whether it can be used a on a per feed level.

    Such as:
    add_filter( ‘wp_feed_cache_transient_lifetime’, create_function( ‘$a’, ‘return 1800;’ );
    $feed = fetch_feed( ‘http://example.com/feedurl’ );
    remove_filter(‘wp_feed_cache_transient_lifetime’, ‘hourly_feed’);

    //Next Feed with diff. cache time
    add_filter( ‘wp_feed_cache_transient_lifetime’, create_function( ‘$a’, ‘return 4800;’ );
    $feed = fetch_feed( ‘http://example.com/feedurl’ );
    remove_filter(‘wp_feed_cache_transient_lifetime’, ‘hourly_feed’);

  4. Frank Avatar

    @r-a-y: to the DB, table _options, not on webspace in a directory

  5. r-a-y Avatar
    r-a-y

    Hmm… ok thanks Frank.

    Not a fan of caching RSS feeds to DB.

  6. […] So, I started to search and ask how often the fetch feed function pulls in the new feeds. I could not, however, find an answer to my question. That’s because I was asking the wrong question. The WordPress feed cache is what determines how often the feed is pulled in. In my search for answers I finally found this article by WP Engineer – Feed Cache in WordPress. […]

  7. Dheeraj Singh Avatar

    How do I default rss caching to a directory and not to the database?

  8. Frank Avatar

    No, only with a plugin.

  9. Dheeraj Singh Avatar

    Which Plugin will do that!!???

  10. Frank Avatar

    It give different plugins, please search on the official site, one of this W3 Total Cache

  11. Jacek Avatar

    But how I can show only one item per feed if I’m using multifetch

    I’m using this chunk of code to array list of feeds
    $rss=fetch_feed(array(
    ‘http://wrzesien.waw.pl/?feed=rss2’,
    ‘http://bala-gan.blogspot.com/feeds/posts/default’,
    ‘http://news.google.com/?output=atom’,
    ‘http://rss.slashdot.org/Slashdot/slashdot’,
    ‘http://rss.cnn.com/rss/cnn_topstories.rss’,
    )):

  12. chris Avatar
    chris

    will the feed options filter only work in debug mode?

  13. Mikael Avatar

    Is there any more tag I have to use o can I just add:

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

    to my functions? This is not working for me at my site.