Quick Fix for WordPress ob_end_flush()  Error

Solving "failed to send buffer of zlib output compression" errors in WordPress

From time-to-time I see the following error on your WordPress website, usually at the very bottom of your pages below the footer.

ob_end_flush(): failed to send buffer of zlib output compression (1)

This happens when your WordPress theme or plugins use output buffering but don’t flush the output properly or, in some cases, when there is an issue with zlib compression of the buffer itself. The general recommendation I see for fixing the error is to remove the built-in wp_ob_end_flush_all() function from the shutdown action hook. This function exists for a reason, and you shouldn’t remove it just to avoid a PHP notice. Here’s a better way to go about solving your “failed to send buffer of zlib output compression” errors in WordPress.

Best Way to Fix It

Instead of removing the wp_ob_end_flush_all() entirely, replace it with the following bit of code to preserve the underlying functionality and remove the PHP notice at the bottom of your WordPress site.

/**
 * Proper ob_end_flush() for all levels
 *
 * This replaces the WordPress `wp_ob_end_flush_all()` function
 * with a replacement that doesn't cause PHP notices.
 */
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
add_action( 'shutdown', function() {
   while ( @ob_end_flush() );
} );

Though it should be obvious, you can paste this into your theme’s functions.php file. It’s always better to solve the underlying problem rather than remove the cause.

Source of the Error

As I mentioned above, 99% of the time this error is triggered by the following function that hooks into WordPress’ shutdown action:

/**
 * Flush all output buffers for PHP 5.2.
 *
 * Make sure all output buffers are flushed before our singletons are destroyed.
 *
 * @since 2.2.0
 */
function wp_ob_end_flush_all() {
  $levels = ob_get_level();
  for ( $i = 0; $i < $levels; $i++ ) {
    ob_end_flush();
  }
}

The specific line causing the PHP Notice is the call to ob_end_flush();. WordPress is checking for the depth of output buffering levels and attempting to flush each one. PHP.net’s Manual documents the return of ob_end_flush() as a boolean, and mentions that it will report any failures as an E_NOTICE. They also mention that:

Reasons for failure are first that you called the function without an active buffer or that for some reason a buffer could not be deleted (possible for special buffer).

PHP.net Documentation > ob_end_flush()

In the case of the notice provided by WordPress, there’s an underlying issue with zlib compression. I definitely wouldn’t recommend that you turn off compression just to get rid of a notice, nor would I recommend that you avoid flushing unresolved output at the shutdown action. This is one of those rare cases where it’s totally fine to use the @ Error Control Operator to disable the notice for this specific case.

Meet the Author

Kevin Leary, WordPress Consultant

I'm a freelance web developer and WordPress consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.