How to enable Output Compression in WordPress using PHP

When it comes to compressing HTML documents in WordPress, you can make things better by adding extra code to your .htaccess file. If you don’t like the method, you can leave the same job to always trustworthy PHP. We all know the power of PHP, so handling Output Compression shouldn’t be a difficult task for it. And it isn’t.

A single line of code in index.php file will do the trick:

  1. Locate the root directory of your WordPress installation
  2. Navigate to index.php and open the file
  3. Copy and Paste the following code on top of the document:

ob_start('ob_gzhandler');

But, allowing this piece of code to do its magic takes one more step. And that is enabling it in php.ini file. Don’t worry; it’s not a big deal:

Set zlib.output_compression = On

You can change level of compression by altering number in the following function:

zlib.output_compression_level = d

“d” can be any number from 1-9 where 1 represents lowest level of compression and 9 the highest one.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *