Codex

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

Difference between revisions of "Output Compression"

(Enable via PHP code: - just added an explanation for users to understand it is WP's main index.php and not index.php of themes)
m (updated link to current version (2.4) of Apache documentation (was linked to 2.0))
 
(One intermediate revision by one other user not shown)
Line 4: Line 4:
 
}}
 
}}
   
WordPress on its own does not offer a mechnism to compress the blog's ''HTML output''. This normally is a feature provided by the webserver (Apache, see [http://httpd.apache.org/docs/2.0/mod/mod_deflate.html#enable Enabling Compression]) or based on the configuration the PHP scripting language offers (see [http://www.php.net/manual/en/zlib.configuration.php zlib configuration]).
+
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 [https://httpd.apache.org/docs/2.4/mod/mod_deflate.html Enabling Compression]) or based on the configuration the PHP scripting language offers (see [http://www.php.net/manual/en/zlib.configuration.php zlib configuration]).
   
 
== Enable via .htaccess ==
 
== 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 usefull:
+
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>
 
<IfModule mod_deflate.c>
Line 47: Line 47:
 
[...]
 
[...]
   
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.
+
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 [http://www.php.net/manual/en/zlib.installation.php Zlib Compression Functions] in PHP in order to get this work.
 
You need to have the [http://www.php.net/manual/en/zlib.installation.php Zlib Compression Functions] in PHP in order to get this work.

Latest revision as of 16:44, 27 September 2016

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