WP-Cache2 and Gzip

WP-Cache2 is a great way to speed up high-trafficked WordPress sites by caching their output as static pages. The only downside is that you have to turn off gzip compression in WordPress, which can actually make some pages slower to download. Wouldn’t it be great if you could have the best of both worlds? It appears that you can.

I think the problem was that with WP’s gzip turned on, it was caching the gzipped output. I edited wp-cache-phase1.php and put in the following:

if ( extension_loaded('zlib') ) ob_start('ob_gzhandler');

right above this line:

foreach ($meta-»headers as $header) {

And it seems to work beautifully. That caches the plaintext, but gzips the text before delivering on cached hits. Might work for people who can’t mess with server configuration files.

You can see it in action over at the WP Hooks Database.

31 thoughts on “WP-Cache2 and Gzip

  1. I tried this but WP_Cache2 plugin is couching up this error:

    Warning: GZIP compression is enabled in WordPress, wp-cache will be bypassed until you disable gzip compression.
    WP-Cache is Disabled

  2. Hello Mark,
    The latest version of WP-Cache is 2.0.17; and if it detects GZip, with or without your mod above, it will disable caching. So, I’m not too sure what you mean by comment #7…..

  3. WP-Cache2 disables caching if it detects that you have enabled gzip compression from within WordPress. With my mod you are doing the compression from within WP-Cache2 and so you should turn of gzip compression in WordPress. The problem is that if you do the compression in WordPress, it gets cached as compressed code. By doing in in WP-Cache2, you can save the cache as plaintext, but compress it when you deliver it.

  4. Ouch… Thanks for clearing that up..
    I’m glad I checked back here for a reply, because I had gone ahead and implemented your patch, then removed all gzip checks from Gallir’s code, and on top of that enabled WP gzip….. It “worked” great until I realized it wasn’t caching anything :P
    Thanks Mark!!!

  5. Pingback: Connected Internet
  6. Strange… I wonder why this isn’t just made an option in wp-cache. So you disable gzip in WP itself and enable again from wp-cache options panel.

    Seems to be working for me.

  7. Kamran: Read carefully, please. You have just to add the line to wp-cache-phase1.php, that’s all… DO NOT enable gzipping in WP options panel, leave it switched off. Then take a look at HTTP headers sending by your server – gzipping works. Tested on WordPress 2.1.3 + WP-Cache 2.1.1

Comments are closed.