Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/public/wp-includes/plugin.php on line 571
Force Refresh SimplePie Cache on WordPress

Force Refresh SimplePie Cache on WordPress

// April 12th, 2011 // Posted in PHP // Tagged as , ,

Introduction

When I tried to build my RSS Feed fetcher for my tweets and Google Reader on WordPress 3.1, I found out that WordPress simplifies the whole process with the built-in function fetch_feed. It nicely instantiate a simplepie object and defines the cache at the same time. By default the cache has 12 hours (43200 seconds) lifespan. You can see it in the “wp-includes/feed.php” from the root of your WordPress installation directory. The nice thing about the cache is that every time someone visit your page SimplePie doesn’t have to refetch all the RSS feeds. The down side is of course every time I update my feed it won’t be picked up immediately up until 12 hours later. Considering the benefit, I don’t want to disable the cache or set the cache lifespan to zero.

Implementations

  • What I want is:
  • – How to refresh the WordPress SimplePie cache
  • – Find a simple way to manually refresh the cache

For the first issue the solution is to attach an extra filter to WordPress event hook “wp_feed_cache_transient_lifetime”. You can put it in anywhere after the WordPress framework got loaded. I put it in the header.php of my theme.

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

For the second issue I can only think of one simple solution and that is by adding an extra HTTP GET variable to the URL. It works by passing special argument to the URL. The extra GET will tell our “refresh cache” code to execute. For example, I can add “?simplepie=refresh” in the URL so whenever I want to refresh my cache all I have to do is to go to “http://www.phacks.net?simplepie=refresh”

Of course it’s not the most secure solution. People can abuse this by constantly going to your URL and force-refreshing your cache assuming they know what the URL is. But for me it’s good enough.

To implement the extra HTTP GET there are couple solutions. The first one would be adding another extra add_filter so WordPress can parse our custom GET, or I could just hard-coded it directly into my theme. I’ll go with the second one since my aim is to have the most simplest solution and yet quite effective.

With that in mind all I have to do is get the GET value using $_GET, and maybe check whether it’s in there or not. I put this code in the header.php of my theme:

if( isset( $_GET['simplepie'] ) && $_GET['simplepie'] == 'refresh' ){
    add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', 'return 0;' ));
}

Now every time I want to refresh my SimplePie cache all I have to do is put extra “?simplepie=refresh” in the URL, like http://www.phacks.net/?simplepie=refresh. I normally do this manual refresh after I updated my Google Reader feed or add new tweet on my Twitter.

Ads by Amazon.com:

Please share this journal with others:

2 Responses to “Force Refresh SimplePie Cache on WordPress”

  1. Nate Greuel says:

    Thanks for this, it’s exactly what I was looking for!

Leave a Reply