How to Enable Output Compression in WordPress

Once you install WordPress and start creating documents which you want to present to your visitors, you might start worrying about the size of your HTML files. As you might already know, even blank spaces, carriage returns, new lines, and tabs take some space. That means a document will take some extra time loading and it will take much more of your bandwidth; that’s something you want to avoid at any costs.

That’s where Output Compression comes as a hero. Because WordPress does not compress files this way on its own, it’s your task to make a conversation with your WordPress system and politely ask it to start compressing.

There are two ways of enabling Output Compression. In this article, we’re about to show you how to:

How to enable Output Compression and remove unnecessary data from your HTML document

If you aren’t stranger to the .htaccess file in WordPress, this method shouldn’t be a problem.

  1. Open .htaccess file
  2. Copy and paste the following code:
  3. # Insert filters
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/x-httpd-php
    AddOutputFilterByType DEFLATE application/x-httpd-fastphp
    AddOutputFilterByType DEFLATE image/svg+xml
    
    # Drop problematic browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
    
  4. Save changes

After you’re done with changes, you are also done with enabling Output Compression which will make your HTML documents much lighter. This simple task will speed up your site, so don’t be afraid of completing the steps.

How to enable Output Compression in WordPress using PHP

If you don’t like the .htaccess method or you aren’t comfortable enough around the file, you can leave the same job to always trustworthy PHP. We all know how powerful PHP is, 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. You will need this line of code:

Set zlib.output_compression = On

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

zlib.output_compression_level = d

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

Conclusion

Enabling output compression in WordPress is as easy as copying and pasting the functions. Still, if you’re not sure about the code or tampering the files, we suggest that you leave the compression to WordPress plugins. After that, you might be interested in clearing and optimizing the WordPress database.

Don’t forget that speeding up a site is an ongoing job, so make time for the optimization of your site on a monthly basis.

Start Blog Book

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 *