I'm using buffer method for final html output recommended by kfriend here https://stackoverflow.com/a/22818089/3465265
It works fine but have issues with cache plugins. Arne L recommended a filter for WP Super Cache here: https://stackoverflow.com/a/51171913/3465265
But that's not working. When i use wp_cache_ob_callback_filter
as filter name, there are no changes at all. No super cache files are generated.
WP Rocket also have similar filter called rocket_buffer
https://docs.wp-rocket.me/article/774-rocketbuffer
Applying that filter name fix minifying issue. HTML and CSS both are minified properly by WP Rocket but no html files are generated inside /cache/wp-rocket directory.
Here is my code:
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('rocket_buffer', $final);
}, 0);
add_filter('rocket_buffer', function($output) {
//this is where changes should be made
$pattern1 ='~(<img.*? alt=")("[^>]*>)~i'; // alt empty
$alt_keyword = "BUBBLE";
$replace_fkw = '$1'. $alt_keyword .'$2';
return $content = preg_replace($pattern1, $replace_fkw, $output);
});
It supposed to replace img empty alt tag with given keyword which works fine except for the issue mentioned with cache plugins.
Any solution please.
Thanks