WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Output Compression

WordPress on its own does not offer a mechanism to compress the blog's HTML output. This normally is a feature provided by the webserver (Apache, see Enabling Compression) or based on the configuration the PHP scripting language offers (see zlib configuration).

Enable via .htaccess

If you prefer to enable or fine tune output compression with the apache server, you might find this code-snippet for an .htaccess file useful:

<IfModule mod_deflate.c>
# 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
</IfModule>

Enable via PHP code

In case editing the servers configuration is not an option for your site you might want to enable compression within the PHP-code. A single small line of code within the index.php file will do the job. Just add ob_start('ob_gzhandler'); on top of your index.php located in the root directory of your WordPress installation:

<?php
ob_start('ob_gzhandler');
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */
[...]

NOTE: For better results, you should edit your main index.php file, not the index.php of your theme(s). Therefore, keep in mind that this file will be overwritten next time you update WordPress, so you need to modify it again after an update.

You need to have the Zlib Compression Functions in PHP in order to get this work.

See Also