I am using wordpress and using the the action 'template_redirect' to start an output buffer of the html content before sending to the browser.
i have imported a library the converts SCSS to css code in php https://scssphp.github.io/scssphp
here is an example of the code
$compiler = new ScssPhp\ScssPhp\Compiler();
add_action( 'template_redirect', 'replace_scss', -100 );
function replace_scss(){
ob_start( function( $buffer ) {
$scss = 'body { p { color: red; } }';
$css = $compiler->compile($scss);
if(!empty($css) && is_string($css)) return preg_replace('scss_compiled', $css, $buffer );
else return $buffer;
});
}
edit:// the following function above when working correctly output
body p { color: red; }
however i am getting the following error.
PHP Fatal error: Cannot destroy active lambda function in scss_compiler\src\Formatter.php on line 287
i assume its because in formatter.php it mentions ob_start; and you cant start ob_start when its already in a callback.
im wondering is there any other way to get the content of the html, make the changes then return to the browser?
i solved my own answer.
I commented out
ob_start();and replaced$out = ob_get_clean();with$out = ob_get_contents();as for the code