0

I would like to be able to remove stuff from the HTML of the page eg. replacing all comments in the body tag or remove som unnecessary code.

Is there a way to create a filter like that getting all the content of the page? not only the post content, all content with footer, main, sidebar, header and so on.

  • Clear your question. Give specific items name or example which you want removed and show what you've tried till now. – CodeMascot Nov 16 '16 at 6:52
1

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);
});
?>
| improve this answer | |
  • This works beautifully as a plugin :) – Jarmerson Mar 1 '17 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.