Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active April 12, 2019 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/f93cc9f152cbde3b24287e4f92bbbfaf to your computer and use it in GitHub Desktop.
Save westonruter/f93cc9f152cbde3b24287e4f92bbbfaf to your computer and use it in GitHub Desktop.
Demonstration of output buffering being non-cancellable to capture the full WordPress response
<?php
/**
* Plugin Name: Non-Cancellable Output Buffer
* Plugin Author: Weston Ruter
* Author URI: https://weston.ruter.net/
*/
namespace OutputBufferTest;
function process_buffer( $response ) {
return $response . sprintf( '<!-- Buffer size: %d -->', strlen( $response ) );
}
// Prevent PHP Notice: ob_end_flush(): failed to send buffer.
remove_action( 'shutdown', 'wp_ob_end_flush_all', 1 );
// Start output buffering when template starts rendering.
add_action(
'template_redirect',
function () {
ob_start(
__NAMESPACE__ . '\process_buffer',
null,
0 // Neither CLEANABLE, FLUSHABLE, nor REMOVABLE.
);
},
PHP_INT_MIN
);
// This will not have any effect.
add_action( 'wp_footer', 'wp_ob_end_flush_all' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment