skip to Main Content

I’m writing a WordPress plugin that obtain the HTML contents

i found 2 ways to obtain the final output:

// first way

function __myplugin_callback($buffer) {
    return $buffer;
}

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

function __myplugin__buffer_end() { ob_end_flush(); }

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

That works when i have only my plugin active, when i have more plugins active, like SEO Yoast, i get conflicts between plugins and mine is not working properly.

// second way
ob_start();

add_action('shutdown', function() {
$final = '';
$levels = ob_get_level();

for ($i = 0; $i < $levels; $i++)
{
    $final .= ob_get_clean();
}

echo apply_filters('final_output', $final);
}, 0);

// call the filter

add_filter('final_output', function($output)
{
     return str_replace(.., .., $output);
});

This works better than the first way but i have also conflicts with plugins like Visual Composer

There is a safe way, to obtain the final html output without conflicts with other plugins?

2

Answers


  1. Chosen as BEST ANSWER

    I made this and i think this is the best way to obtain and change the output without plugin conflicts.

    function myplugin_output_start()
    {
        ob_start("myplugin_output_parse");
    }
    
    add_action("registered_taxonomy", "myplugin_output_start", 0);
    
    function myplugin_output_parse($output)
    {
        if(ob_get_level() > 1) {
            return $output;
        }
    
        // statements...
    
        return $output;
    }
    

  2. Output buffering seems like an odd way to do this. Unless I’m missing something, you should be able to use file_get_contents():

    $page_content = file_get_contents( $_SERVER['REQUEST_URI'] );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search