Hiding Wordpress version using wp_generator

Viewed 555

I've tried to remove the generator meta from a wordpress 5.6 installation but it will fail. I've added this hook to my functions.php file (also in child theme that is active) but without success

remove_action('wp_head', 'wp_generator');

What I've missed out?Is there any change in wp 5.6 api that will break this hook?Can be the issue caused from caching plugins like jetpack?

1 Answers

You can use, as an alternative, the the_generator() function through a filter.

the_generator( string $type )

Display the generator XML or Comment for RSS, ATOM, etc.

Parameters Description
$type (string) (Required) The type of generator to output (html/xhtml/atom/rss2/rdf/comment/export)

The following should hide the generator meta <meta name="generator" content="WordPress 5.6" />

add_filter( 'the_generator', function() {
  return;
} );

Keep in mind that the version is also appended to each Wordpress core script. You can add the following to hide the version.

<?php
$filters = [
  'style_loader_src',
  'script_loader_src',
];
foreach ( $filters as $filter ) {
  add_filter( $filter, function( $src ) {
    if( strpos( $src, '?ver=' ) )
      $src = remove_query_arg( 'ver', $src );
    return $src;
  } );
}; ?>

PHP > 7.1 required, Anonymous function used in example

Related