Wordpress filter to modify final html output

2019-01-10 09:59发布

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. Anyone know of one?

I've browsed the list of filters a number of times, but nothing jumps out at me: http://adambrown.info/p/wp_hooks/hook/filters

(I've tapped some Wordpress-specific communities for this question, but not having received a single reply, thought I'd turn to the venerable SO.)

标签: wordpress
10条回答
做个烂人
2楼-- · 2019-01-10 10:01

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

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

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 STILL supports it, you should not use it.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-10 10:02

Try to solve it with JavaScript first before using output buffers.

Let's say you want to add a spinner while loading iframes, for example:

(function ($) {

    $(document).ready(function () {
        showSpinnerWhileIFrameLoads();
    });

    function showSpinnerWhileIFrameLoads() {
        var iframe = $('iframe');
        if (iframe.length) {
            $(iframe).before('<div id=\'spinner\'><i class=\'fa fa-spinner fa-spin fa-3x fa-fw\'></i></div>');
            $(iframe).on('load', function() {
                document.getElementById('spinner').style.display='none';
            });
        }
    }

})(jQuery);

Adapt that idea to your needs, usually you should be able to manipulate HTML output with JavaScript without the need to mess with output buffers, that can break cache, for instance.

查看更多
萌系小妹纸
4楼-- · 2019-01-10 10:08

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:

<?php
// example from php.net
function callback($buffer) {
  // replace all the apples with oranges
  return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html><body>
<p>It's like comparing apples to oranges.</p>
</body></html>
<?php ob_end_flush(); ?>
/* output:
   <html><body>
   <p>It's like comparing oranges to oranges.</p>
   </body></html>
*/
查看更多
Explosion°爆炸
5楼-- · 2019-01-10 10:14

the question is maybe old, but i have found a better way to do it.

function callback($buffer) {
  // modify buffer here, and then return the updated code
  return $buffer;
}

function buffer_start() { ob_start("callback"); }

function buffer_end() { ob_end_flush(); }

add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');

Explanation This plugin code registers two actions – buffer_start and buffer_end.

buffer_start is executed at the end of the header section of the html. The parameter, the callback 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, and callback, so they do not conflict with other functions you may have in plugins.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-10 10:16

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.

查看更多
7楼-- · 2019-01-10 10:20

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.

查看更多
登录 后发表回答