[Solved] How to edit the html of frontend code before output on wordpress.


Basically, WordPress is not built in a way to allow you that. It just goes on pushing its output whenever it wants, and when any of your filters is executed it is either before any output happened (so you have nothing to modify) or after (so it is already gone away to the browser).

There is an option which you could try: using output buffering. You can start your own output buffer on init, catch it on shutdown, and use this last opportunity to modify the output.

The general outline is as follows:

add_action('init', 'ob_start');
add_action('shutdown', function () {
    $html = ob_get_clean();
    $hmtl = morbvel_modify_html($html);
    echo $html;
});

AFAIR, WordPress already has an action on shutdown which cleans all output buffers. It is up to you to check its source code to see what priority to assign to your shutdown hook to make it work.

PS. As a WordPress user myself I wouldn’t like a plugin to do something like this – I’ll be afraid that your code spoils something in my awesome html output I have carefully crafted. But you may turn out as lucky as to find your faithful customers.

1

solved How to edit the html of frontend code before output on wordpress.