Home  >  Article  >  CMS Tutorial  >  How to remove WordPress version number using code

How to remove WordPress version number using code

藏色散人
藏色散人forward
2020-04-27 16:50:073271browse

The following column WordPress Tips will introduce to you how to use code to remove the WordPress version number. I hope it will be helpful to friends in need!

How to remove WordPress version number using code

By default, WordPress will output the version number in the header of the page, which has certain security risks.

Add the following code in functions.php of the current theme to remove the WordPress version number in feed and js/css at the same time:

// 同时删除head和feed中的WP版本号
function ludou_remove_wp_version() {
  return '';
}
add_filter('the_generator', 'ludou_remove_wp_version');
// 隐藏js/css附加的WP版本号
function ludou_remove_wp_version_strings( $src ) {
  global $wp_version;
  parse_str(parse_url($src, PHP_URL_QUERY), $query);
  if ( !empty($query['ver']) && $query['ver'] === $wp_version ) {
    // 用WP版本号 + 12.8来替代js/css附加的版本号
    // 既隐藏了WordPress版本号,也不会影响缓存
    // 建议把下面的 12.8 替换成其他数字,以免被别人猜出
    $src = str_replace($wp_version, $wp_version + 12.8, $src);
  }
  return $src;
}
add_filter( 'script_loader_src', 'ludou_remove_wp_version_strings' );
add_filter( 'style_loader_src', 'ludou_remove_wp_version_strings' );

Recommended tutorial: " WordPress

The above is the detailed content of How to remove WordPress version number using code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete