/ Updated 10 July 2022 / 1 min read
Remove Yoast HTML Comments in Version 11.0
If you were previously stripping Yoast HTML comments on your website then there is a good chance that updating to v11.0 will show the new schema data at the top of your website.
The problem is that a widely used snippet for stripping out the comments searched for the string ‘yoast’ and then removed the surrounding comment tags. The new schema data added in V11.0 uses the word ‘yoast’ as a class on the script tag, so the script tags get stripped and the schema data shows at the top of the website.
We can fix this by making the search term more specific so that it strips the comments but misses the schema class.
//Remove Yoast HTML Comments
//https://gist.github.com/robwent/f36e97fdd648a40775379a86bd97b332
function go_yoast() {
if (defined('WPSEO_VERSION')){
add_action('get_header',function (){ ob_start(function ($o){
return preg_replace('/\n?<.*?Yoast SEO plugin.*?>/mi','',$o); }); });
add_action('wp_head',function (){ ob_end_flush(); }, 999);
}
}
add_action('plugins_loaded', 'go_yoast');