Dramatically Speed Up WordPress/WPMU Load Times With This Simple Tip!

050514-F-7203T-005.JPGWho doesn’t want lightning fast page delivery? Is your WPMU site operating at a painfully, embarrassingly slow speed? People are impatient when it comes to site load times and will often leave if it takes longer than two seconds to see the page. The information available to internet users is increasing exponentially and people have no time to waste on slow loading websites. Optimizing your WordPress blog for speed is a serious matter of competition for the attention of your user. The faster you can serve your pages, the better chance you have of lowering your bounce rate. This small piece of code will take you less than a minute to paste into your site and can instantly have your site running approximately 3-4 times faster than before. It’s called zlib compression.

What is zlib Compression?

zlib is a lossless data-compression library that will help you to serve your pages faster. Basically it compresses the PHP that WordPress sends to your visitor’s browser so that it displays much more quickly. Compressing and sending is much faster than sending the PHP in its full size format. Believe me, you will love it.

How do I use it?

Step 1:

First, do a quick check to make sure that zlib is enabled on your server. Paste this into a text file, save as it test.php, and upload to your server:

1
<?php phpinfo(); ?>

Visit your test page and it should display everything you ever wanted to know about PHP running on your server. Scroll down until you find the zlib header and check to see if it’s enabled. If it is, then you can proceed to the next step. If zlib isn’t enabled, Twitter Tools & Apps, contact your host. I can’t guarantee that they will enable it for you, but the change in speed is so significant that it’s worth a try.

Step 2:

Add this small bit of code to the header.php file of your theme. Place it directly above your doctype at the beginning of the file.

1
2
3
4
<?php
ini_set(’zlib.output_compression’, ‘On’);
ini_set(’zlib.output_compression_level’, ‘1);
?>

Update the file and the change should be instant. You will probably notice it even before you use tools to test the output. I recently tested zlib with one of my WPMU sites and it helped me to serve my pages 3.5 times faster than before compressing- an unbelievable improvement! You can check your compression factor at: http://www.port80software.com/support/p80tools.asp. It will tell you if compression is working on your site and the percentage and number of bytes saved, as seen below one of my sites.

speed

I first learned about it from Brad Ney in his helpful post. It’s important for you to know that this technique is compatible with WP Super Cache, which is a plugin that I highly recommend for anyone running WPMU. It causes your blogs to serve static html pages to the majority of your visitors, specifically those who are not logged in, have never left a comment or have never viewed a password-protected post. This plugin can really save the day if you or any of the bloggers on your WPMU network have a post that gets a lot of attention and you experience a sudden spike in traffic. If your server can’t handle that amount of traffic, you can cause a crash during what ought to be your shining moment. Get your site prepared before this happens. If you’re using shared hosting that is completely oversold and overloaded, this plugin will also give you an instant boost.

These speed-enhancing methods are not just WPMU-compatible but will dramatically increase the speed of a single-user WordPress blog as well. You may not be able to afford higher quality hosting, but now you may not have to. These tools should improve your site’s load time with any host. They are also compatible with all major browsers, even the ancient ones. Combine zlib compression with WP Super Cache and you are on the road to a super fast loading site!

Comments (58)

  1. I checked the PHP info on my WAMPServer on my machine and the zlib info checked out so I inserted the snippet of code at the very top of the header.php file. Ended up with a screen full of gobbledy-gook. Don’t know offhand what the problem is there, may have something to do with other PHP settings or just because it’s a Windows server.

    Then I tested it out on a site that I have hosted elsewhere (running on a Linux server) and it worked perfectly.

  2. Hey Sarah – Thanks for the reference!

    I’m glad you found that useful – I know I sure did… I’m currently enjoying a 3x page loading improvement.

    Its a shame though that my web host hasn’t enabled gzip! Or I would of been sending .CSS and .JS files compressed as well. :D

  3. I too am wondering how you would implement this on a WPMU with over 100 themes (without having to place the code in each theme manually, and again, each time the themes are updated). I have tried two WPMU global header plugins but to no avail. Also does it matter if WPMU is installed via a subdirectory?

    Thanks!

  4. ^Disregard *Noob alert*…

    I answered my own question, just gzipped my entire site and that took care of the subdirectory (duh) install.

    Amazing results!

    Original size: 25980 bytes

    Compressed size: 7530 bytes

    Savings in bytes: 18450 bytes

    Percentage saved by compression: 72.0%
    Transfer speed improvement: 3.4 X
    ———————————————
    Here’s what I added to my .htaccess file (my top-level .htaccess, not the WPMU .htaccess)

    #BEGIN SITE COMPRESSION#

    # compress all text and html:
    AddOutputFilterByType DEFLATE text/html text/plain text/xml

    # Or, compress certain file types by extension:

    SetOutputFilter DEFLATE

    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

    #END SITE COMPRESSION#

  5. Thanks for the tip.

    Also, with WP Super Cache, is there any way to check if its incompatible with certain plugins? I have so many plugins installed, I don’t know what might break with WP Super Cache.

  6. I tried this but get the error on a white screen:
    parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /[...]/header.php on line 11

    I checked and zlib is enabled on my server.

    Any idea why this could be? Thanks for your help.

  7. Kunal – that’s weird. If you copied and pasted the code in there, it’s possible that the single quotes were pasted in the wrong format. Delete the single ticks and then manually enter them from your keyboard – if that makes sense. ;) WordPress formats it strangely when I post the code so that quotes don’t paste right.

  8. Wow, I can’t thank you enough for this extraordinarily helpful tip, Sarah! We got a 78% compression and 4x improvement in load times!

    You have become a must-read for me every day now. Thank you, thank you, thank you.

  9. Superb stuff! Thank you so much for this awesome little hack, works like a charm!

    I too had to change the little ticks ( ‘ ) individually as I got the (parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /[...]/header.php on line 11) error but once I changed it – wow.

    I cannot thank you enough.

    Please if you get a moment Sarah, hop on board Freelancer Unplugged too if you freelance at all :D

  10. Hi Sarah
    I took a look at Brad Ney’s article on using zlib and then found your article.

    Your article has clarified a few issues… such as setting up the “test.php” to check for zlib and placing the code in the header PHP “Place it directly above your doctype at the beginning of the file”

    Having read both articles I should be able to give it a go.

  11. Ed Morgan – probably your issue is that you need to replace the single quotes in the code I posted. It probably pasted in a formatted way. Just go through and delete each tick and type it in manually, if that makes sense.

  12. Adding this along with wp super cache wont really add any benefit.

    This compresses php, as where wp super cache serves html. It might just be my morning blues though :S

    Its one of the things i love about php though :P

  13. I have checked to see if this was enabled and it say yes… I talke with host gator and they said the code needs to go in the php.ini file and not the header.php of the theme.. .

    i have wordpress MU installed and would like to do this also… not sure what code or where to past now… they said the sytax of the code in this article would not work in a php.ini file…

    help apprecaited.. thanks

  14. Im having problems with this. I followed the instructions. It says zlib is enabled. So i added that code at the top of the header. As soon as i upload that file I am not able to see the site anymore. I get a blank screen :S
    Please help :)
    Thanks!

  15. If this is what it says about my server:

    zlib.output_compression Off
    zlib.output_compression_level -1
    zlib.output_handler no value

    does it mean that zlib is not enabled?

    • @Anna Lynge
      from the above comment i can tell you that zlib compression is not enabled but as sarah has told in previous comment that you can use gzip also so try that one :-)

  16. Hi, great tip right enough Sarah.

    I’m on Bluehost and zlib is enabled – thankfully, so I follwoed the steps and out the code in the header.php before the doctype.

    I did notice the single quotes were squint – i.e. set at an angle out the way, so manually changed them – they type in vertical, and it worked first time.

    hope that helps anyone..

    cheers..

  17. Hi,
    I also not sure if my Zlib is working, This is what the test.php saying:
    ————————
    ZLib Support enabled
    Stream Wrapper compress.zlib://
    Stream Filter zlib.inflate, zlib.deflate
    Compiled Version 1.2.3
    Linked Version 1.2.3

    Directive Local Value Master Value
    zlib.output_compression Off Off
    zlib.output_compression_level -1 -1
    zlib.output_handler no value no value
    ————————
    ZLib Support=enabled but the zlib.output_compression=Off
    What is that mean?

    If It doesn’t work on my server and I would like to use gzip, Should I use this code on the HEADER.PHP ?:
    ————————

    ————————

    Thanks.

  18. Hi,
    Im also not sure if my Zlib is working, This is what the test.php saying:
    ————————
    ZLib Support enabled
    Stream Wrapper compress.zlib://
    Stream Filter zlib.inflate, zlib.deflate
    Compiled Version 1.2.3
    Linked Version 1.2.3

    Directive Local Value Master Value
    zlib.output_compression Off Off
    zlib.output_compression_level -1 -1
    zlib.output_handler no value no value
    ————————
    ZLib Support=enabled but the zlib.output_compression=Off
    What is that means?

    If It doesn’t work on my server and I would like to use gzip, Should I use this code on the HEADER.PHP ?:
    ————————
    -?php
    ini_set(’gzip.output_compression’, ‘On’);
    ini_set(’gzip.output_compression_level’, ‘1′);
    ?-
    ————————

    Thanks.

  19. Thanks for the awesome speed up tip …

    But as i can see from this article:
    (http://bradblogging.com/tutorials/speed-wordpress-up-3-times-as-fast-under-a-minute/)

    the man says that if Gzip is enabled, i use the code from here: (http://webdesign.about.com/od/speed/ht/website-compression.htm)

    i can’t benefit the speed up optimization yet i can’t see any problems when adding the Zlib code + i can see from the test that it indeed speeds up my wordpress installation.

    And as @bytheweb says, i get the same text when checking if Zlib is enabled, so is it?

Participate