Created
March 13, 2024 18:27
-
-
Save jeremysimmons/f53de9c43a212e2a819167d091e330c3 to your computer and use it in GitHub Desktop.
Replace the output in php
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 | |
// https://stackoverflow.com/a/22818089/26877 | |
// wp-content/mu-plugins/buffer.php | |
/** | |
* Output Buffering | |
* | |
* Buffers the entire WP process, capturing the final output for manipulation. | |
*/ | |
ob_start(); | |
add_action('shutdown', function() { | |
$final = ''; | |
// We'll need to get the number of ob levels we're in, so that we can iterate over each, collecting | |
// that buffer's output into the final output. | |
$levels = ob_get_level(); | |
for ($i = 0; $i < $levels; $i++) { | |
$final .= ob_get_clean(); | |
} | |
// Apply any filters to the final output | |
echo apply_filters('final_output', $final); | |
}, 0); | |
<?php | |
add_filter('final_output', function($output) { | |
return str_replace('foo', 'bar', $output); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment