Post Pic

Compress WordPress output and speed your blog’s load speed

WordPress, by default, comes uncompressed and sends the uncompressed HTML to the visitor’s browser. With one line of code added to your header, you can compress WordPress’s output by up to 75%. By using zlib compression technology, you can harness the power of PHP and reduce your blog’s load speed!

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.

Related Posts

Related Posts

No related posts.

52 Responses

Oct 21 2009 10:02

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.

Oct 21 2009 10:44

Thanks for sharing. I like this, I will try to make it. :)

Oct 21 2009 11:01

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…

Oct 21 2009 12:02

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

Oct 21 2009 12:23

Well, it doesn’t work on my server :(

Oct 21 2009 14:34

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!

Oct 21 2009 14:56

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.

Oct 21 2009 20:51

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.

Oct 21 2009 21:02

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?

Oct 21 2009 21:08

i get:
Warning: ini_set() [ref.outcontrol]: Cannot change zlib.output_compression – headers already sent in /home//header.php on line 2

Oct 21 2009 21:50

yey, will give it a try too to see if really works for me. Thanks.

Oct 21 2009 22:02

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.

Oct 21 2009 22:25

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

Oct 22 2009 09:04

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

Oct 22 2009 11:08

A very quick tutorial, hope it works for me too. Thanks…

Oct 22 2009 14:56

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″

Oct 22 2009 20:39

@ Rarst – That might be very worthwhile to figure out to do. JS and CSS are big bandwidth hogs :D

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

Oct 25 2009 19:03

Thanks. I will test it out and see how much faster it makes my site.

Oct 26 2009 01:05

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.

Oct 29 2009 12:33

Anyone have any idea???

Nov 01 2009 20:23

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?

Nov 03 2009 23:37

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.

Nov 04 2009 00:33

@ 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!

Nov 04 2009 00:34

@ Devin – Are you sure you didn’t accidentally delete more than just the code? Perhaps you deleted a div class by mistake?

Nov 04 2009 00:46

No, actually because no divs are above or close to the DOCTYPE area.

Nov 04 2009 21:33

@ 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?

Nov 05 2009 00:39

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?

Nov 09 2009 15:36

First, it displayed a page full of garbage. Then something like “site not found”. :(

Nov 15 2009 19:21

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.

Dec 09 2009 07:14

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.

Dec 09 2009 13:29

So how would I decompress the files?

Dec 09 2009 16:47

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:

Dec 09 2009 16:49

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 ?

Dec 17 2009 20:47

thanks for your tip, i added into my website.

Dec 26 2009 22:32

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!

Jan 25 2010 21:49

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?

Jan 25 2010 23:21

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

Feb 18 2010 07:45

Thanks for the tips.but will there be any problem with compressed files already present in download section of blog.like in format .rar,.zip

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required