title

Storing Your Whole Page In A Variable

 

Storing Your Whole Page In A Variable

23 Oct 2013, Posted by antoine in

In some cases, storing your whole output in a variable can be very helpful. This allows you to make global changes, compress or obfuscate code and more very easily. All we need is PHP output buffering and two hooks.

add_action('wp_head', 'smashing_buffer_start');
add_action('wp_footer', 'smashing_buffer_end');
function smashing_buffer_start() {
ob_start( 'smashing_callback' );
}
function buffer_end() {
ob_end_flush();
}
function smashing_callback( $content ) {
// Feel free to do things to the content here
$content = str_replace( 'great', 'awesome', $content );
echo $content;
}