DQuinn.net

WordPress

Make WP Super Cache Play Nice with Preview Draft and Update Profile in WordPress

If users update the information in their profiles, that information won't appear on the site until WP Cache gets cleared.

WP Super Cache is one of those magical plugins that does all the work for us without our having to think. We like plugins like this. We all know WordPress is a fat, resource intensive hogbeast because it has to make so many queries to the database, so when we’re hit with a spike in traffic (see: “The Digg Effect“), chances are that the whole thing might implode.

How do we get around this? Well, WP Super Cache creates static html files of our posts so that repeat visitors in a short time span don’t force WordPress to engage the database in order to serve posts and pages. Donncha O Caoimh, the creator, describes the plugin’s functionality best:

99% of your visitors will be served static html files. Those users who don’t see the static files will still benefit because they will see regular WP-Cache cached files and your server won’t be as busy as before. This plugin will help your server cope with a front page appearance on digg.com or other social networking site.

This is all fine and good, and works beautifully.* One small problem with WP Super Cache, however, is that if users update the information in their profiles, that information won’t appear on the site until WP Cache gets cleared. Also, the second time you Preview a saved draft, you’ll be seeing your last saved draft because it got cached.

So the caching drafts is a problem, but why should we care about the cached profile? Well, I ran into this problem on Rokkyuu Magazine, because users can change the color scheme of the site by setting an option in their profile. So even though they may have updated their profile, when they returned to the site, they were seeing a cached version (and their previously chosen color scheme). Moreover, at Rokkyuu, what users enter in their profile while logged in to the Dashboard appears in a members directory on the public site, which also gets cached.

To get around this, and with a bit of Donncha’s help, I added the following to functions.php:

What this does is hook the wp_cache_no_postid(0); function, which clears WP Cache, to the function that gets called when a user updates her profile.

As for the Drafts…

To fix the drafts, we do a similar thing. I couldn’t figure out how to hook into the functions that get called when you click “Preview” post or “Save Draft,” but I did find a hook that does the job.

Adding this to functions.php clears the cache whenever I click Preview post, thereby enabling me to see my latest edits with WP Super Cache enabled.

* Plugins that display up-to-the-minute information using PHP like WP-UserOnline, for example, won’t necessarily “not work” as Donncha notes on the plugin page: they’ll just be displaying information that’s 3600 seconds old.


Also in WordPress


Post a Comment

Your Two Cents

Reply

Your Comments

2 Replies & Counting

  1. nomadic11 9 years, 1 month ago

    Hello

    This is very useful.

    Though in case you turn off supercache, the code in functions causes problems.

    It would need a if(function_exists(‘…’))

    But I couldn’t figure out what call … is used for supercache

    thanks

  2. dquinn 9 years, 1 month ago

    Good point. I think Donncha implemented this fix into the latest version of the Supercache plugin, however, so it shouldn’t be necessary any longer.