HOWTO: Make WordPress plugins work with WP Super Cache

Daffodil, snow and water droplets Thaya Kareeson has written an excellent article for plugin developers. It goes through how to make plugins work with WP Super Cache by using dynamic AJAX calls.

WP Super Cache can make static html copies of pages served by WordPress which is great for performance. Unfortunately that means some plugins don’t work because they rely on executing PHP on each request. The plugins need to be rewritten to use AJAX calls by the visitor’s browser. There’s a FAQ in the readme.txt all about it!

I previously wrote about adding AJAX to WordPress plugins but Thaya has worked through a simple example that will work perfectly with WP Super Cache. It’s a good foundation for plugin developers start from.

He also has versions of WP Postviews and Popularity Contest that have been rewritten to support static caching. I haven’t tried either plugin so leave a comment on his blog if you need help!

If you depend on a large portion of your content being dynamic this isn’t the solution for you as it will affect what search engines see. Those bots don’t speak Javascript, but for interactive purposes (ratings, stats etc) it’s the job.

reading glasses Further reading:

  • AJAX in Plugins is a must-read starting point for developers.
  • wp_enqueue_script() is the command WordPress uses to load Javascript files. That page links to a couple of good pages too including this best practices post. As of this writing, Thaya’s example don’t use wp_enqueue_script() but it’s simple to use.

The vast majority of plugins work just fine with WP Super Cache, but some of the ones that don’t are quite popular. If your favourite plugin doesn’t work, why don’t you help the author out and fix it? You have all the source code after all and you’ll be helping everyone else who uses the plugin!

WP Super Cache 0.9.1

WP Super Cache version 0.9.1 is now available. WP Super Cache is a page caching plugin for WordPress that will significantly speed up your website.

Major changes under the hood in this release, and many bugfixes:

  • If your blog is installed in a folder then compare the mod_rewrite rules in your .htaccess with those on the admin page. I fixed a bug in how those are generated.
  • Out goes the shutdown function the plugin relied on for years (going back to the days of wp-cache), and in comes plain old output buffering on it’s own.
  • If you’ve had problems clearing the cache on your blog it could be because wp-cron isn’t firing. I’ve added checks for that. Joost helped me debug that and he blogged about it too. You’ll get a nice warning message if those checks fail.
  • If after all that, your cache still doesn’t clear, add $wp_cache_shutdown_gc = 1; to wp-content/wp-cache-config.php to clear the cache at the end of pageload instead of by wp-cron. It will slow down page generation for a tiny number of your users though.
  • The Last and Next garbage collection times are now in the timezone selected for your blog.
  • Added an admin notice on the plugin page to warn that caching has to be enabled. A warning is shown below the plugin activation row too.
  • If your site runs on a Windows server, I fixed a small problem with slashes and creating the config file.
  • The plugin created empty supercache folders, but that’s fixed now.
  • Bad Behaviour support seems to work nicely now!
  • You can now relocate the supercache plugins folder. See $wp_cache_plugins_dir in wp-cache-config-sample.php.
  • I added 2 new filters: wp_cache_served_cache_file in wp-cache-phase1.php (BB uses this) and wp_cache_file_contents in wp-cache-phase2.php where you can filter the contents of the newly created cache before it’s written out to a file.
  • The readme.txt has been updated too warning about using NFS to store the cache folder, solving wp-cron problems, added the list of Apache modules required for expired pages to really expire in the browser cache.

I also added a donation link in the readme.txt and on the admin page. You can hide it with the click of a button but if you’re feeling generous, I’d appreciate a donation.
I don’t expect many donations, that’s how these things work, but if you tell me your site does 100,000 page views a day and you couldn’t live without caching I might be slightly annoyed if you come looking for free support.

PS. Looks like Bad Behavior support is broken because the docs on the BB site were a little misleading and I don’t use the plugin. Grab badbehaviour.php and copy into plugins/wp-super-cache/plugins/ overwriting the file of the same name in that folder.

WordPress and Blacknight power the best Irish blogs

grandad

Last Saturday night many of the Irish blogging community got together in The Cork International Airport Hotel for the Irish Blog Awards. I was there with my wife Jacinta, it was a great night and I was delighted to see so many WordPress powered blogs. Both WordPress.org and WordPress.com blogs were represented again and again among the top bloggers in Ireland.

I had planned on writing a post analysing the breakdown of WordPress vs Blogspot vs MT vs everyone else but lucky for me, Cathal Garvey already did the hard work!

pub

He has some very interesting stats on version numbers, and I’m shocked that “Best Technology Blog” (sponsored by Bitbuzz) has the worst record for upgrading WordPress. Hopefully this attention will encourage those bloggers to upgrade.

WordPress is the most popular blogging software among the Blog Awards nominees and Blacknight is by far the most popular hosting service. I don’t think you can get a better recommendation than that for either WordPress or Blacknight.

BTW – That’s Grandad in the photo above. Go read his blog, Head Rambles. You’ll love it!
PS. Here are the results of the Awards, read more by Red Mum, Robert, Niall and Jason who reminds us we should vote for the excellent Cork International Airport Hotel. TheChrisD has a massive post listing everyone in his feedreader who posted about the Awards, a few pics, and some of the videos shown on the night.
I’ll be uploading photos from the night in a post here and to Pixie later.

PPS. Alan Burke got in touch and asked me to give Drupalcamp Galway a plug. He added, “Maybe we could convert a few WordPress users ;-)” but of course that’ll never happen!

On Saturday 4th and Sunday 5th of April 2009 in Galway, 2 groups of Drupal developers will work on building 2 real working Drupal sites for 2 good causes. They will have 24 hours to complete their website. We are calling organizations, charities and NGOs from Ireland to submit their site proposal. Any organisation can register as long as they are not-for-profit and do not have a Drupal site already running. (A few static HTML pages is the limit for being accepted).

Sounds like a great idea and a worthy one too.

The survivors guide to unemployment

jobless I like this. David Jones was recently laid off from his day job and decided to write a book about his experiences in the hope that it might help others in the same position. The book is called “Oh No: I’ve Lost My Job What Am I Going To Do?” and was written in just a few days.

Last week he was on Newstalk talking about his book when he was contacted by Maryrose Lyons from Brightspark Consulting who suggested that a website would really help his book. I’ll let Gordon continue, he’s one of the team who built the site:

Maryrose gathered a team together consisting of Rachel Earley (Website Graphic designer) , David McDonald (Book Graphic Designer), David Duignan (Photographer), Alex Gibson (Award winning Podcaster ) and myself to put the site together and make the functional stuff like Paypal work.

And so was born Joblessandproud.com. Today, for one day only, you can download David’s book for free. It’s only €4.99 anyway, a steal at that price.

It also has a blog, I’m glad to see powered by WordPress. I’m going to subscribe and see what David makes of this opportunity. It’s wonderful to see that WordPress is part of this venture.

WordPress MU 2.7

After a lengthy delay, WordPress MU 2.7 is now available. WordPress MU is the software that runs on WordPress.com and many other sites.

This is a sync of the code from WordPress 2.7 released last December. It’s taken a while but I think it’s worth the wait as there are many bugs fixed. Besides the merge with WordPress 2.7 there are also a few new features, including Viper007bond’s admin bar which is the most user visible addition.
Check the timeline for further info.

Upgrading from WordPress MU 2.6.5 should be as easy as copying over your existing install but please backup everything. Blog databases must be upgraded but that will occur in the background as usual.

I would really like to thank everyone who helped me bring this release out and offered me encouragement along the way. I’d name names but I’m bound to miss someone! It’s great hearing what everyone is doing with WordPress MU. There’s quite a community out there!

WP Super Cache 0.9

WP Super Cache version 0.9 is now available. WP Super Cache is a page caching plugin for WordPress that will significantly speed up your website.

Update – if your blog is installed in a sub directory instead of at the root of your server, the .htaccess file might be wrong. Download and install the development version which corrects this. You’ll have to edit the .htaccess file (anyone want to volunteer to write an auto-upgrader?) and remove the rules between the lines # BEGIN WPSuperCache and # END WPSuperCache. Delete those lines too and visit your admin page. The plugin should do it’s stuff and spit out correct mod_rewrite rules for you. The dev version also has a few other bug fixes so if you’re adventurous have fun upgrading!
There’s no need to touch wp-content/cache/.htaccess

Are we nearing version 1? Possibly. In this release a number of bugs have been fixed and the following new features added:

  1. Mobile user agents are detected and a different cache page created for them. If you use a plugin that displays a different theme to these devices it will hopefully work now. The plugin changes to “half-on” mode because the detection is done by PHP. Thanks to Alex King‘s WordPress Mobile Edition plugin for the detection code.
  2. The number of cache files will be more than halved on a normal site with plenty of anonymous visitors. This should be a huge win for WordPress MU sites and very busy blogs.
  3. The last and next garbage collection times are displayed on the admin page now.
  4. In previous versions the newly generated page was delivered uncompressed to the first visitor even if the browser supported compression. I’ve fixed this but I’m not 100% certain it will work for everyone so it’s disabled by default. Edit wp-content/wp-cache-config.php and add “$wp_cache_gzip_first = 1;” to enable this. It’s enabled here.
  5. If expiry time is longer than 30 minutes, garbage collection will be done every 10 minutes, else it’ll be done 10 seconds after the expiry time.
cache-contents

Unfortunately wp-content/advanced-cache.php will have to be updated again and it’s not as simple as copying the file from the plugin directory. Full instructions are printed on the admin page and it should auto update in most cases.

PS. Test your upgraded blog on Is my blog working, a project by fellow Automattican Alex Shiels.
Here’s the page for this site. 102ms page generation time is rather fast!

WP Super Cache 0.8.9

WP Super Cache version 0.8.9 is now available. WP Super Cache is a page caching plugin for WordPress that will significantly speed up your website.

This version fixes a number of bugs and adds two new features, but in case you run into problems upgrading, make sure you delete wp-content/advanced-cache.php before copying plugins/wp-super-cache/advanced-cache.php over it. Go on, read that again. Delete that file. Jonathan Dingman didn’t and when he copied the file over the symlink he overwrote wp-super-cache/wp-cache-phase1.php. His site went belly up and he started screaming for my head!!! Err no, he appeared on asking why it didn’t work. Thanks Jonathan for working through the problem with me. My fault for not making the instructions clearer!

If this happens to you, take a deep breath and unzip the plugin again in the plugins folder and delete wp-content/advanced-cache.php

Anyway, the new features are:

  1. Cache rebuild. Serve a static cache file to anonymous users while that file is being generated.
  2. Disable the large global lock that makes every access to the cache atomic.

Besides those, I got rid of the symlinked file in wp-content/, and the plugin now copies a file called “advanced-cache.php” from the plugin directory to simplify things. Especially on operating systems where symlink isn’t available. Yes, that’s Windows.

If your site is horrendously busy and you get a ton of comments every day and you’re too broke to buy a new server, then the cache rebuild feature will help you a lot. You should see the load on your server go way down if you enable this. Anonymous users who visit a page where a comment has just been left will be served a static cache file from the supercache instead of all the requests trying to generate a brand new page. The page served to them might be a few seconds out of date but that trade off is worth it.
Here’s the original thread that inspired the idea. Thanks Tigertech for writing the patch and for sharing the performance graphs. Check out the load on his server, before and after the rebuild function was switch on:

wp super cache load graph

If you’ve had problems with deleting the cache on your blog it *might* be because of file locks. Some hosts just have problems with them. The file locking in the plugin is very coarse. When the plugin wants to do any sort of write operation it grabs a lock, does the writes (which could include clearing expired files, or creating wp-cache and supercache cache files), and then releases the lock.
Any other process running the plugin that tries that won’t get a lock: new cache files won’t be created, and cache files won’t be cleared.
It isn’t a huge problem that the lock is so coarse because the writes don’t take very long (the lock is enabled after the page is generated), but some very busy sites take quite a while to clear their cache files.
On the downside, disabling the file lock won’t stop multiple cached files being generated simultaneously (great), but it also won’t stop multiple “clear cache” attempts either (boo!)
From what I recall of looking at the other cache plugins for WordPress, most of them don’t have any file locking and seem to do just fine.

I almost forgot! There is also a new debug mode. Edit wp-content/wp-cache-config.php and look for “$wp_cache_debug” and follow the instructions. It will send you a few emails when things don’t go right and may help track down any problems.

As a final note, I would like to sincerely thank Robert Wolf who spruced up the admin page and gave it a nice lick of paint and Michael Torbert for helping me debug the plugin a while back.

WordPress MU 2.7 beta

A beta version of WordPress MU 2.7, the multi blog version of WordPress that runs on such sites as WordPress.com has just been uploaded and is available here.

This is quite a stable release and has been tested by many but it’s not the final 2.7 release so there are probably bugs we haven’t found yet. Don’t put it into production just yet, but if you’re working on a new site, you’re better off using this rather than 2.6.5 as the final WordPress MU 2.7 will be along soon.

Check the timeline for more info, there are still plenty of open tickets and keep an eye on the forums and please help out if you can.

I also want to extend a big thank you to everyone who has helped with this release, be it entering tickets, updating them, creating patches, or helping out on the forums and of course the WordPress community. This software wouldn’t be where it is today without you.

WP Super Cache 0.8.7

WP Super Cache version 0.8.7 is now available. WP Super Cache is a page caching plugin for WordPress that will significantly speed up your website.

This release addresses some issues large or busy sites had with deleting expired cache files. Some sites generated so many cache files that there were two many files generated, even when those files were cleared out every hour. To fix that I limited the recursive function that deletes the files to 100 deletions at a time. That function is called until all the cache files are deleted (or strictly speaking, no files are deleted. That’s how the expired file cleanup works).
It’s also possible to set the garbage collection interval to 10 minutes now which will also help.

WordPress 2.6 Easter Egg

This is new to me, even though others have known about it for months. There’s a Matrix Easter Egg in WordPress 2.6 and in the betas of WordPress 2.7. To activate it:

  1. Edit a post.
  2. Scroll down to the revisions section of the page and click the latest revision.
  3. On the next page, scroll down again and select the same revision for comparison.
  4. The Matrix has you..

Danger !
Self-comparison detected.
Initiating infinite loop eschewal protocol.
Self destruct in… 3
2
1
Wake up, Donncha…
The Matrix has you…
Don’t let this happen again. Go Back.