Last active
April 12, 2019 16:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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