How to edit the html of frontend code before output on wordpress.
Asked Answered
E

1

-2

It's specifically i'm developing a plugin but stuck to edit the front end html. Is there any filter or function of wordpress to edit the html of a page?

Evzone answered 1/2, 2018 at 15:28 Comment(0)
W
3

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.

Wachtel answered 1/2, 2018 at 21:33 Comment(1)
I'm using output buffering in my project as some function does echo instead of returning, it's a good thing but it does not return the dom in good php way like JavaScript, so my code maybe too much ugly to do the operation like to add html in somewhere, due to this buffering I also think speed will be slow. But anyways it's a good answerEvzone

© 2022 - 2024 — McMap. All rights reserved.