Last updated on September 25, 2023

Enable GZIP Compression

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Improve performance with GZIP in WordPress.

Enabling GZIP compression in WordPress can significantly improve the performance of your website by reducing the size of files sent from your server to the user’s browser. This can result in faster page load times and improved overall user experience.

To enable GZIP compression in WordPress, you can add the following code snippet to your theme’s functions.php file:

function wpsnippets_enable_gzip_compression() {
    if ( !ob_start("ob_gzhandler") ) {
        ob_start();
    }
}
add_action( 'init', 'wpsnippets_enable_gzip_compression' );

This code snippet uses the ob_start() function to start output buffering and enable GZIP compression. It first attempts to use the ob_gzhandler function, which is a built-in PHP function for GZIP compression. If the ob_gzhandler function is not available, it falls back to the default output buffering.

By adding this code snippet to your theme’s functions.php file, GZIP compression will be enabled for your WordPress site.

Examples

Example 1: Enabling GZIP Compression using .htaccess

This use case demonstrates how to enable GZIP compression in WordPress by adding the necessary code to the .htaccess file.

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

This code snippet adds the necessary directives to the .htaccess file to enable GZIP compression for various file types. It uses the mod_deflate module to compress the specified file types, resulting in reduced file sizes and faster page load times.

Example 2: Enabling GZIP Compression using a Plugin

This use case demonstrates how to enable GZIP compression in WordPress using a plugin called “WP Super Cache”.

/**
 * Enable GZIP compression using WP Super Cache plugin.
 */
function wpsnippets_enable_gzip_compression() {
    if (function_exists('wp_cache_gzip_compression')) {
        wp_cache_gzip_compression();
    }
}
add_action('init', 'wpsnippets_enable_gzip_compression');

This code snippet utilizes the wp_cache_gzip_compression() function provided by the WP Super Cache plugin to enable GZIP compression. It hooks into the init action to ensure that the function is called during the initialization process of WordPress.

Example 3: Enabling GZIP Compression using a Custom Function

This use case demonstrates how to enable GZIP compression in WordPress using a custom PHP function.

/**
 * Enable GZIP compression using a custom function.
 */
function wpsnippets_enable_gzip_compression() {
    if (!ob_start("ob_gzhandler")) {
        ob_start();
    }
}
add_action('init', 'wpsnippets_enable_gzip_compression');

This code snippet uses the ob_start() function with the “ob_gzhandler” callback to enable GZIP compression. It starts an output buffer and applies the GZIP compression handler to the buffer. If the GZIP compression is not supported, it falls back to a regular output buffer. The function is hooked into the init action to ensure it is executed during the initialization process of WordPress.

Last updated on September 25, 2023. Originally posted on October 15, 2023.

Leave a Reply

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