preg_replace on all HTML content in WordPress

WordPress doesn’t have a “final output” filter.

Just google search I found a interesting answer from Stack Overflow.

All credits go to @kfriend

More Details (SO question link) : WordPress filter to modify final html output

<?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);

An example of hooking into the final_output filter:

<?php
add_filter('final_output', function($output) {
    return str_replace('foo', 'bar', $output);
});
?>