Was looking for a solution for removing Layer Slider meta generator, didn't find much help on any of the handful of websites I looked at, they are all sharing the same info, which only pertains to either WordPress generator or popular plugins like WooCommerce.
The problem here is that every plugin is going to have it's own hook names and naming conventions, so to learn or know them all will be nearly impossible. The easiest way I think is plain PHP with preg_replace
.
Working code that has been tested in WordPress 6.5.
Inside functions.php of your theme drop in this code and it should work.
//Remove All Meta Generators
ini_set('output_buffering', 'on'); // turns on output_buffering
function remove_meta_generators($html) {
$pattern = '/<meta name(.*)"generator"[^>]*>/i';
$html = preg_replace($pattern, '', $html);
return $html;
}
function clean_meta_generators($html) {
ob_start('remove_meta_generators');
}
add_action('template_redirect', 'clean_meta_generators', 100);
add_action('wp_footer', function(){ ob_end_flush(); }, 100);
I am using regular expression to capture the meta
tag. It covers whether they put spaces in between the equals sign or not. Using ob_start
to cover the whole document. So we add the preg_replace
starting at the header all the way to the footer. See how ob_start
works in the PHP Manual, also there are times WordPress codex does states it should be using ob_start
.
If you find this useful please add a thumbs up so the next person looking can land on a working solution that covers all meta generators. I feel it's bad security for these plugin and platform developers to put meta generator version numbers in the code. Especially with evolving vulnerabilities being discovered all the time.
Ive also added a plugin that does this exact thing on the WordPress repository.
Remove Meta Generators