Get entire page content (generated HTML in browser)

I’m looking for a filter that provides the full page generated in WordPress – including the head section with meta tags, body, styling and dynamic content. the_content() and the_excerpt() do work only for the dynamic part and the request filter seems to work only for SQL queries (or not)?

I would like to filter some data in the entire page in all links or buttons. How can I do this?

Related posts

Leave a Reply

3 comments

  1. You can use output buffering to accomplish this.

    Add a high priority hook directly before the template is rendered:

    add_action('template_redirect', 'foo_buffer_go', 0);
    function foo_buffer_go(){
        ob_start('foo_buffer_callback');
    }
    

    Add a shutdown hook with an extremely low priority.

    add_action('shutdown', 'foo_buffer_stop', 1000);
    function foo_buffer_stop(){
        ob_end_flush();
    }
    

    Inside your callback, you manipulate the rendered HTML.

    function foo_buffer_callback($buffer){
      //Do something with the buffer (HTML)
      return $buffer;
    }
    

    You may download this as a plugin here: http://3-3.me/B9lK

    1. Enable the plugin
    2. Visit the site and it will be rendered as “Foo Bar” which means you are capturing the entirety of the generated HTML
  2. Slight change to @BrianFegter’s code so the output buffer is closed only if it was opened. This has the benefit of being able to conditionally decide whether to filter or not.

    if ($want_to_modify_content) {
        add_action('template_redirect', 'YOURPLUGIN_buffer_start', 0);
    }
    function YOURPLUGIN_buffer_start() {
        add_action('shutdown', 'YOURPLUGIN_buffer_stop', PHP_INT_MAX);
        ob_start('YOURPLUGIN_modify_content'); 
    }
    function YOURPLUGIN_buffer_stop() {
        ob_end_flush();
    }
    function YOURPLUGIN_modify_content($content) {
        //modify $content
        return $content;
    }