Remove meta-generator tags from WordPress using PHP


On 02-23-2017 articles, code

WordPress is one of the most popular CMS on the web today. But there are a few really bad practices that both WordPress core and plugin developers have, which is putting the version number of their software in the html code. With the ever growing vulnerability database that is WordPress, please stop helping people with ill intent by giving away what version you are using. I doubt people will stop doing this as they are trying to get better brand recognition, but in the end it’s a security problem!

There is some code that I have written over the years to help remove version numbering from WordPress but sometimes the plugin developer likes to use the meta-generator tag. For turning this off on WordPress there is a function you can run, and well with so many plugins it’s gonna be hard to know or remember all the hooks or function names to over ride it.

So the simple solution? Just use plain PHP to remove any meta-generator tag. Here is the code you will use inside your functions.php of your theme.

//Remove All Meta Generators
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('get_header', 'clean_meta_generators', 100);
add_action('wp_footer', function(){ ob_end_flush(); }, 100);

So basically this is using preg_replace to find the meta-generator tag and replace it with nothing, which will remove it. ob_start is a way to run this function from the header to the footer covering the whole document output. And don’t think that this can only be used with WordPress you can use it on pretty much any platform. Just use the ob_start function and run a preg_replace from top to bottom. Of course you will have to customize the code to fit your other framework system you are using.

But I suggest you look at the ob_start function it’s pretty nifty when you are trying to clean out unwanted text inside of html.

http://php.net/manual/en/function.ob-start.php

By the way this post was written because I was looking for a solution and I didn’t find any online, they were all geared toward just removing the WordPress meta-generator. I actually share this code on a the stackoverflow forum as a solution to another person’s question. Since the answers giving weren’t really the solution he was looking for. Please thumbs up the forum post if you find this helpful. You can find it here WordPress – remove Meta generator tags

Comment Via Facebook

Speak your mind

*