Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created November 6, 2019 15:12
Show Gist options
  • Save bdeleasa/fe5bfc3ed20b303a0d6baca4d6184e45 to your computer and use it in GitHub Desktop.
Save bdeleasa/fe5bfc3ed20b303a0d6baca4d6184e45 to your computer and use it in GitHub Desktop.
Meant to be used as a Wordpress MU plugin. Uses output buffering to add a new filter that parses the entire final HTML output of each page.
<?php
/**
* Plugin Name: WP HTML Buffer
* Description: Uses output buffering to add a new filter that parses the entire final HTML output of each page.
* Plugin URI: https://briannadeleasa.com
* Version: 1.0.0
* Author: Brianna Deleasa
* Author URI: https://briannadeleasa.com
* Text Domain: wp-html-buffer
*/
/**
* 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( 'wp_final_html', $final );
}, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment