First, place the following code in a file and call it "test.php" and then upload it to the root of your blog directory:
<?php phpinfo(); ?>
Make sure that "zlib" is enabled by your hosting provider.
Second, place the following code in your header (above the DOCTYPE):
<?php
ini_set('zlib.output_compression', 'On');
ini_set('zlib.output_compression_level', '1');
?>
You're done! Check Port80Software.com to ensure you are compressing your output.
22/10/2009 update : If you're interested in WordPress themes designed for small businesses, you should definitely have a look to the list I have compiled over at cats Who Blog!
Thanks to Brad Ney for hios contribution to WpRecipes! Brad is the author of BradBlogging.com, which is all about WordPress, HTML & CSS and Usability tips and tweaks. You can subscribe to his RSS Feed here.
51 Responses
Use this tip with care, it isn’t a blanket “performance increase”
If you have an underpowered web server (processor bound) and a busy website, this can actually slow your site down as each request then has to be zipped by the processor.
A more solid solution would be to use wp-super-cache which compresses the output once and writes this to disk, then automatically serves back the compressed file without having to recompress each time.
Thanks for sharing. I like this, I will try to make it.
I rememebr that option being a part of wordpress but it was taken out. then a couple of plugins sprang up to put that feature back into wordpress so if you do not feel like editing your theme, google for those plugins.
I’d also suggest to check if compresion is enabled on your server already, your host might already do the compression for you… via apache modules…
Don’t forget to remove (or block) phpinfo afterwards, no reason to show whole world precise server configuration.
I also find it more convenient to set zlib flags in htaccess or php.ini (depends on server configuration).
Well, it doesn’t work on my server
thanks for that tip. i knew there is zlib to compress thing but didn’t know that you can use it here within wordpress! cool thing!
Thanks for the tip never thought to actually check into this. Generally I just assume Apache is doing the gzip’ing for me but assuming is never a good idea.
I get the delorean, but the married couple?
Any who, nice trick, will give that a try and see my output. May help now that I am on my on VPS.
I currently use WP Super Cache with GZIP. Would this recipe be a good addition to it? What is the difference between GZIP and ZLIB?
i get:
Warning: ini_set() [ref.outcontrol]: Cannot change zlib.output_compression – headers already sent in /home//header.php on line 2
yey, will give it a try too to see if really works for me. Thanks.
Hmm just tried, works but yeah just for html, so unless you’ve got loads of html code in a page, not really worth it.
html download with T1: 0.31s without code, 0.24s with code.
Good to know for huge wp html content.
ps: sorry for double post.
@ Paul – Have you pasted the code twice or have zlib compression enabled for your entire Wordpress blog?
@ Kevin – You are correct. The CSS and JS are a different story, I plan to find a way to compress them as well.
@Brad
You can use zlib for CSS and JS but that is tricky to setup. The way I do it is adding Apache handler to treat those as PHP (so zlib kicks in) and prepend with PHP code to override headers so type changed back for browser.
Hard to setup but I like that it doesn’t require to change actual CSS and JS file, most of other methods do.
A very quick tutorial, hope it works for me too. Thanks…
Also gave me an error on archives pages:
“arning: ini_set() [ref.outcontrol]: Cannot change zlib.output_compression – headers already sent in (…)/wp-content/themes/tma/header.php on line 2″
@ Rarst – That might be very worthwhile to figure out to do. JS and CSS are big bandwidth hogs
@ kevin – You still haven’t told me if you pasted the code twice, or if you have the zlib compression set up in the php.ini – Thanks.
Thanks. I will test it out and see how much faster it makes my site.
Hey, this thing kind of messed up my post listings. It only shows 2 posts from the category page.
How do I fix it, I already removed the file and the line of code.
Anyone have any idea???
I didn’t post the code twice but I am facing similar issues as faced by Kevin..
I am on VPS and zlib is enabled.. pasted the code in header.php and got the error. Any helpful suggestions?
A better idea would definitely use a plugin, which already makes gzipped files and stores them on the server – WP Super Cache for instance as was pointed in an earlier comment.
That way it doesn’t have to be compressed every time.
@ Rockstar & Kevin – Your zlib compression might already be set in one of two places:
1) .htaccess file
2) php.ini (usually set up by the web host)
Let me know if you find anything in those files, if you have access to them.
Thanks!
@ Devin – Are you sure you didn’t accidentally delete more than just the code? Perhaps you deleted a div class by mistake?
No, actually because no divs are above or close to the DOCTYPE area.
@ Devin – I’m detecting some hostility.
My mistake, I was writing another tutorial for another website and had div classes in my mind. You are correct.
I’m pretty sure that this code doesn’t affect Wordpress once you take it out.. perhaps it is a new/rogue plugin?
No actually because I would have seen the error 100% before I tried this tutorial.
Some people I talked too said that it did something to the DB?
Do you think that’s true?
First, it displayed a page full of garbage. Then something like “site not found”.
Thank you for this superb post. But I had trouble navigating around your website as I kept getting 502 bad gateway error. Just thought to let you know.
The CSS Compress wordpress plug-in will automatically compress your CSS files with GZIP, that combined with this ZLIB compression tip for HTML code can make noticeable difference on your page load times.
I found that the tweetmeme, digg.js and other social submit buttons that call external .js sources account for over 6 seconds of latency on my blog load time, not sure how to speed up load times for .js files that are hosted from external sites and sources.
So how would I decompress the files?
If zlib isn’t working for you, try using gzip. It’s a little more popular and easier to use because instead of creating a file and uploading it, all you have to do is paste in one line of code above the doctype.
I wrote about it here: wpbloghost.com/blog/gzip-compression
The code you’ll want to paste in above the doctype is:
Sorry, my code didn’t show up. Use this:
?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip‘)) ob_start(”ob_gzhandler”); else ob_start(); ?
Make sure to put a at the end after the last ?
thanks for your tip, i added into my website.
I tried this compression method out. My server supports ZLIB, but this didn’t work. I tested it through Port80Software.com just to be sure, but my site wasn’t compressed. I have been looking all over. Do you know a compression method that will work? Thanks!
is it recommended to store repeating functions results in variables?
For example, if I have the_title() or the_permalink() functions appearing more than once in my index.php.
Will it be faster if I type and then echo $title1?
@jack
Naturally it is always faster to use variable than call function. But since WP caches database queries I don’t think there will be considerable difference in this case.
As usual it is balance of speed and convenience. Hard-coded values are fast, functions are flexible.
Trackbacks: