Allow to modify HTML in output buffer
-
Hey!
Here is Matthew, the developer behind Real Cookie Banner. I want to make Real Cookie Banner compatible with SiteGroud Optimizer. A few of our users told us, that the “Content Blocker” does not work with your plugin.
Reason: You are flushing the output buffer and therefore Real Cookie Banner can no longer modify the HTML content.
Solution: Provide a filter to change the output buffer content received by your parser instance: https://gist.github.com/matzeeable/623ed9e96688a3a21b06a6e1d88bd761/revisions#diff-1a7c1850f88287936713a7072af9331818e14581fc71feed100c41e1747593ce
Regards,
Matthew 😎
-
Hey Matthew,
Thank you for your collaboration.
I have reported the case to our developers and they will consider the requested feature.
Best Regards,
Dimitar PetrovAs a Siteground customer and a customer of Real Cookie Banner, I am looking forward to the fix. According to a support chat with Siteground just now, and all being well, this should be applied in the next few days.
Hi @jr104,
We cannot provide an ETA. My colleague from the mentioned chat session has misunderstood the case and considered it as a bug fix request. We apologize for that.
Our developers are reviewing the request in detail and if possible the solution will be implemented in the future versions of the SiteGround Optimizer plugin.
Best Regards,
Dimitar PetrovThanks for the update Dimitar. It shouldn’t take too long to check and test the supplied code, as long as nothing else is misunderstood.
Hey @demiro !
Thanks for your reply.
Since it is a one liner, is there really no evidence of this in terms of ETA?
At the moment we have to advise our customers not to use SPO, because presently it cannot be used GDPR compliant with Real Cookie Banner because the content blocker cannot be applied due to discarded output buffers.
What do you think?
Indeed, your solution is one line.
Our developers are still reviewing the request and note that extensive testing is also required. I’m afraid, we cannot provide an ETA.Best Regards,
Dimitar PetrovHey @mguenter
First we would like to point that we are very happy when other plugin developers are working on making their products compatible with ours. Thank you for the efforts you have put on this so far.
However, what you’re suggesting is not the best way to achieve what you’re after. We’re applying all our optimizations via this parser and adding the option to modify it will endanger the overall functionality of the plugin. That’s why I am afraid we won’t be able to provide the requested filter.
We are using default priority of 10 to open and close/flush the buffer, so you can open a new one with priority of 11 and modify the HTML content after it is being returned from our plugin.
This way some incompatibilities can be avoided and each plugin will handle its own changes only.
Hi @hristo-sg !
Yeah, you are right about the philosophy wrapping multiple output buffers. But: In your coding, you are doing the following:
add_action( 'shutdown', array( $this->parser, 'end_buffer' ) ); /** * End the buffer. * * @since 5.5.0 */ public function end_buffer() { if ( ob_get_length() ) { ob_end_flush(); } }Which leads to that your coding is flushing the existing output buffer. Meanwhile, WordPress registers a similar functionality to flush all available output buffers at
shutdownhook:– https://github.com/WordPress/WordPress/blob/76207c890b68098000be3e54385dea54229a1944/wp-includes/default-filters.php#L401
– https://developer.ww.wp.xz.cn/reference/functions/wp_ob_end_flush_all/What is the reason of using
ob_end_flushwhen you are already working with anob_startcallback AND WordPress is already handling this?And, within our plugin, we are doing the following:
`add_action(‘plugins_loaded’, function() {
ob_start(‘my_callable’);
});From your point of arguments, this should work as expected, as
plugins_loadedis loaded first and afterwardsinitregisters your handler. But I cannot make it work, can you please take a look at this? 🙂Do not rely on the shutdown action to close the buffer. Instead of modifing the output and wait for WordPress to close it, use one of the recommended buffering functions.
The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.
The reason why shutdown action closes the buffering is to prevent forgotten opened buffers, Do not rely on it, it’s there to prevent a white screen…
Hi again @hristo-sg !
Thanks for your follow-up.
Can you please recommend a way to modify the HTML code with SGO? From my previous post, the following code does not work as expected:
add_action("plugins_loaded", function() { ob_start(‘my_callable’); });But
plugins_loadedis definitely beforeinit.add_action( 'init', [$this->getBlocker(), 'registerOutputBuffer'], 11 ); add_action( 'shutdown', [$this->getBlocker(), 'closeOutputBuffer'], 11 ); public function registerOutputBuffer() { if ($this->isEnabled()) { \ob_start([$this, 'ob_start']); } } public function ob_start($response) { // do whatever the plugin does return $modified_html; } public function closeOutputBuffer() { if ( ob_get_length() ) { ob_end_flush(); } }@hristo-sg thanks for your suggestion.
I need to put some more time in here to make sure we don’t get any incompatibility with other plugins in here. Unfortunately I still have to listen to
plugins_loadedbecause e.g. caching plugins change thesrcofscriptsand I have to “wait” for these modifications accordingly (or page builders modifying the content, inject scripts afterwards, and what else there is not…).More and more I’m considering making my own implementation of the output buffer specifically for SGO to be compatible with it. 🙂
The topic ‘Allow to modify HTML in output buffer’ is closed to new replies.