Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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

3 Answers 3

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;
}
share|improve this answer

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
share|improve this answer
    
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

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

share|improve this answer
    
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). –  Rilwis 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) –  Rilwis 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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