5

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?

  • 1
    What exactly are you trying to accomplish? – kaiser Feb 6 '12 at 21:23
  • As I said above - filter some data in the entire page. Which means - receiving (intercepting) page content in a variable, filtering the content as I need and returning the modified content just as the_content filtering works. – Mario Peshev Feb 7 '12 at 7:47
  • Keep in mind that by doing this, the time to first byte for the paste will also be the full PHP execution time, so there is a major performance cost to this that's proportional to the page generation time, no parallel transfer or rendering can happen – Tom J Nowell Dec 21 '18 at 1:07
7

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
  • Tried your snippet but I get: Notice: ob_end_flush() [ref.outcontrol]: failed to delete and flush buffer. No buffer to delete or flush in ... (URL) . And when I try to var_dump the buffer and die, it is empty. Any hints? – Mario Peshev Feb 6 '12 at 16:43
1

You can use HTTP API to get the content of the post URL.

  • I thought about it, but I need the entire page code on every single page. I want to do some content filtering like hiding some inappropriate links or something that could be in the header, content, footer, sidebar and static divs in templates and outputted by plugins. – Mario Peshev Feb 6 '12 at 16:45
  • I understand, and I think this method works perfectly (I met this issue and used this solution). – Anh Tran Feb 7 '12 at 4:19
  • Ah, just rethink about it, and I think you want to change the content of the page when it's loaded, right? Hmm, then it's harder! Maybe filter on all areas is not a good idea. Why don't you do filter on some fixed location, which you can add your custom hooks (custom filter) – Anh Tran Feb 7 '12 at 4:25
  • my assignment is different as I need to filter everything. I could filter header, sidebar, footer etc areas, but different pages use different templates and it might not work in every case. And, certainly, I don't want to modify the core - where I could buffer before and after the index calls (or template rendering inside). – Mario Peshev Feb 7 '12 at 7:48
1

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;
}

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.