WordPress has great filter support for getting at all sorts of specific bits of content and modifying it before output. Like the_content
filter, which lets you access the markup for a post before it’s output to the screen.
I’m trying to find a catch-all filter that gives me one last crack at modifying the final markup in its entirety before output.
I’ve browsed the list of filters a number of times, but nothing jumps out at me:
https://codex.wordpress.org/Plugin_API/Filter_Reference
Anyone know of one?
WordPress doesn’t have a “final output” filter, but you can hack together one. The below example resides within a “Must Use” plugin I’ve created for a project.
Note: I haven’t tested with any plugins that might make use of the “shutdown” action.
The plugin works by iterating through all the open buffer levels, closing them and capturing their output. It then fires off the “final_output” filter, echoing the filtered content.
Sadly, WordPress performs almost the exact same process (closing the open buffers), but doesn’t actually capture the buffer for filtering (just flushes it), so additional “shutdown” actions won’t have access to it. Because of this, the below action is prioritized above WordPress’s.
wp-content/mu-plugins/buffer.php
An example of hooking into the final_output filter:
Edit:
This code uses anonymous functions, which are only supported in PHP 5.3 or newer. If you’re running a website using PHP 5.2 or older, you’re doing yourself a disservice. PHP 5.2 was released in 2006, and even though WordPress (edit: in WP version < 5.2) STILL supports it, you should not use it.
The question is may be old, but I have found a better way to do it:
Explanation
This plugin code registers two actions –
buffer_start
andbuffer_end
.buffer_start
is executed at the end of the header section of the html. The parameter, thecallback
function, is called at the end of the output buffering. This occurs at the footer of the page, when the second registered action,buffer_end
, executes.The
callback
function is where you add your code to change the value of the output (the$buffer
variable). Then you simply return the modified code and the page will be displayed.Notes
Be sure to use unique function names for
buffer_start
,buffer_end
, andcallback
, so they do not conflict with other functions you may have in plugins.AFAIK, there is no hook for this, since the themes uses HTML which won’t be processed by WordPress.
You could, however, use output buffering to catch the final HTML:
@jacer, if you use the following hooks, the header.php also gets included.
I was using the top solution of this post (by kfriend) for a while. It uses an
mu-plugin
to buffer the whole output.But this solution breaks the caching of
wp-super-cache
and no supercache-files are generated when i upload themu-plugin
.So: If you are using
wp-super-cache
, you can use the filter of this plugin like this:Modified https://stackoverflow.com/users/419673/kfriend answer.
All code will be on functions.php. You can do whatever you want with the html on the “final_output” filter.
On your theme’s ‘functions.php’
You might try looking in the wp-includes/formatting.php file. For example, the wpautop function.
If you are looking for doing something with the entire page, look at the Super Cache plugin. That writes the final web page to a file for caching. Seeing how that plug-in works may give you some ideas.
Indeed there was a discusussion recently on the WP-Hackers mailing list about the topic of full page modification and it seems the consensus was that output buffering with ob_start() etc was the only real solution. There was also some discussion about the upsides and downsides of it: http://groups.google.com/group/wp-hackers/browse_thread/thread/e1a6f4b29169209a#
To summarize: It works and is the best solution when necessary (like in the WP-Supercache plugin) but slows down overall speeds because your content isn’t allowed to be sent to the browser as its ready, but instead has to wait for the full document to be rendered (for ob_end() ) before it can be processed by you and sent to the browser.
To simplify previous answers, just use this in
functions.php
:I’ve been testing the answers here now for a while, and since the cache breaking thing is still an issue, I came up with a slightly different solution. In my tests no page cache broke. This solution has been implemented into my WordPress plugin OMGF (which has 50k+ users right now) and no issues with page cache breaking has been reported.
First, we start an output buffer on template redirect:
Then, we apply our own filter to the HTML.
And then we can hook into the output by adding a filter:
Hope it helps anyone.
I have run into problems with this code, as I end up with what seems to be the original source for the page so that some plugins has no effect on the page. I am trying to solve this now – I haven’t found much info regarding best practises for collecting the output from WordPress.
Update and solution:
The code from KFRIEND didnt work for me as this captures unprocessed source from WordPress, not the same output that ends up in the browser in fact. My solution is probably not elegant using a globals variable to buffer up the contents – but at least I know get the same collected HTML as is delivered to the browser. Could be that different setups of plugins creates problems but thanks to code example by Jacer Omri above I ended up with this.
This code is in my case located typically in functions.php in theme folder.