Add Snippet To Project
Has the tag by WordPress at the header of your site ever bothered you? This seemingly harmless tag actually reveals the version of WordPress your site is currently using, which could be a potential security risk as it makes your site an easier target for hackers. If you’re looking to increase your site’s safety, removing this meta generator tag could be beneficial. This article will guide you through the simple yet effective process of erasing the meta generator for a more secure WordPress site.
function wpturbo_remove_meta_generator() {
return '';
}
add_filter( 'the_generator', 'wpturbo_remove_meta_generator' );
The code snippet provided is quite simple and straightforward, yet very powerful in improving the security of your WordPress website.
We begin by creating a new PHP function called wpturbo_remove_meta_generator(). It’s this function that essentially plays the central role in disabling the meta generator tag in WordPress:
function wpturbo_remove_meta_generator() {
return '';
}
The function returns an empty string, effectively replacing what would have been output by WordPress as the meta generator tag.
By default, WordPress outputs a meta generator tag in the head of your website’s HTML output, indicating the version of WordPress you are using. This information may be used by malicious hackers to target your site if it’s running an older, vulnerable version of WordPress. So, it’s generally seen as good practice to remove such easily accessible information about your site.
To apply this function and make it modify the HTML output of your website, we use the add_filter() function. add_filter() is a WordPress function that’s used to modify various types of data in WordPress.
The add_filter() function takes two arguments. The first argument is the name of the filter hook, which is ‘the_generator’ in this case. The ‘the_generator’ filter hook allows you to modify the output of the generator tag in the <head> section of your website.
The second argument is the name of the function to be executed when the filter hook is applied, which is wpturbo_remove_meta_generator() in our situation:
add_filter( 'the_generator', 'wpturbo_remove_meta_generator' );
So, when the ‘the_generator’ filter hook is triggered by WordPress, it calls our wpturbo_remove_meta_generator() function, causing the meta generator tag to be replaced by an empty string, thereby effectively disabling the output of the meta generator tag.
