Mark on WordPress

WordPress puts food on my table.

WP-Cache2 and Gzip

with 26 comments

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.

Written by Mark Jaquith

January 31, 2006 at 8:05 am

Posted in wordpress

26 Responses

Subscribe to comments with RSS.

  1. I’m going to try that out here on WP.com.

    Matt

    January 31, 2006 at 2:30 pm

  2. Hmm, it looks like we’re already doing gzip at the server level, so not needed.

    Matt

    January 31, 2006 at 2:43 pm

  3. Yeah, if you control your server, you can always just do that… I’m hoping that this will be of some use to the many people on shared hosting.

    Mark Jaquith

    January 31, 2006 at 5:57 pm

  4. well, if only WP-Cache worked with WP2, the world would’ve been a much better place!! :)

    Amit Gupta

    February 17, 2006 at 6:57 pm

  5. WP-Cache2 works just fine with WordPress 2.0 and 2.0.1 on every server I’ve installed it on.

    Mark Jaquith

    February 19, 2006 at 4:43 am

  6. 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

    Harshblogger

    March 11, 2006 at 5:22 pm

  7. Disable Gzip compression in WordPress… let WP-Cache2 (modified) handle it.

    Mark Jaquith

    May 2, 2006 at 7:47 am

  8. 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…..

    Computer Guru

    May 3, 2006 at 4:31 am

  9. 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.

    Mark Jaquith

    May 3, 2006 at 4:38 am

  10. 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!!!

    Computer Guru

    May 3, 2006 at 5:11 am

  11. [...] I’ve since installed WP-Cache 2 with the required WordPress 2.0/PHP 5 fix and Mark Jaquith’s gzip compression patch. I tested it to make sure it works, but I’m leaving it off until the next time the barbarians show up at the gate, as it screws with some dynamic code I have and I haven’t figured out how to get it to execute the code every time. Yet. [...]

  12. How To Enable Gzip With WP-Cache2

    I’ve made one last tweak to my site to improve performance that I’d like to highlight to other WordPress users who use the WP-Cache2 plugin. WP-Cache2 unfortunately doesn’t work with gzip compression, which means that some of your pa…

    Connected Internet

    January 2, 2007 at 11:02 am

  13. [...] Mark Jaquith and Chris Stormer have found that a minor change in the the code of the plugin can turn on GZip [...]

  14. [...] Mark Jaquith and Chris Stormer have found that a minor change in the the code of the plugin can turn on GZip [...]

  15. 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.

    Robert Accettura

    April 22, 2007 at 12:52 pm

  16. I tried adding above line , but as soon as i enable gzip, cache gets disabled.

    Kamran

    Kamran

    May 12, 2007 at 1:10 pm

  17. 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

    Honza

    May 16, 2007 at 9:30 am

  18. coolness.. at first im a little bit skeptic trying it out, but yeah, my page did load faster.. good job..

    =)

    dude alert!

    August 4, 2007 at 8:42 pm

  19. [...] your content. It turns out that it only requires one line of code. You can thank WordPress guru, Mark Jaquith, for figuring it out a little over a year and a half ago. Until I get some people to test it, [...]

  20. this does not work when i test it with a compression check, still shows my site as being uncompressed

    mharacz

    September 21, 2007 at 7:40 pm

  21. [...] This was not trivial as the standard WordPress gzip option was interfering with our caching plugin. I found a fix for this though and ran a few tests: the filesize of our HTML code is reduced by around 84%: from around [...]

  22. I wonder If it does allow you to use WP-Polls for example. Do you guys have any experience?

    MeME

    April 17, 2008 at 5:13 am

  23. Thanks this really speeds up my blog

    Web Hosting Coupon Codes

    September 19, 2008 at 7:32 pm


Leave a Reply