Tuesday, March 18, 2014

When WP_DEBUG is set to true, output buffer always includes an ob_end_flush() notice.

When WP_DEBUG is set to true, the output buffer always includes an error. Specifically, the error I see is :

<b>Notice</b>:  ob_end_flush() [<a href='ref.outcontrol'>ref.outcontrol</a>]: failed to delete buffer zlib output compression.

Since I'm making sure my plugins are PHP 5.2+ compatible, hence testing in PHP 5.2, I'm not sure if this is what's causing the notice in my output buffer.

This additional output is included along with my response and is a deal breaker for me. Traced the problem to be coming from an action that is triggered on shutdown. Specifically, a shutdown calls the following code which in turn gives the notice.

function wp_ob_end_flush_all() {
$levels = ob_get_level();
for ($i=0; $i<$levels; $i++)

So, i just remove the action in all my ajax callback functions before I echo the output back to the client eg:

add_action('wp_ajax_prefix_readABC', array('prefix_readABC'));

public function prefix_readABC(){
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1);
echo '{"result":["a", "b", "c"]}';

The above appears to work without problems. Note the inclusion of remove_action( 'shutdown', 'wp_ob_end_flush_all', 1);

It does feel a little clumsy but I'm only doing it in the ajax request and we're only outputting a piece of json back to the client with a shutdown, so I don't seehow this will have any drastic affects anywhere else and the issue is resolved for now. Lastly, this is only done when WP_DEBUG is true, i.e. your in development. I don't have any problems when in production and WP_DEBUG is turned off (false).

If you are using classes then it's just a matter of having a base class with the remove_action in the constructor.