WordPress - 如何删除元生成器标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16335347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:13:48  来源:igfitidea点击:

WordPress - How do I remove meta generator tags?

wordpressmeta-tags

提问by John

I have these tags:

我有这些标签:

<meta name="generator" content="Woo Framework Version 3.1.1" />
<meta name="generator" content="WordPress 3.5" />
<meta name="generator" content="Canvas 3.0" />

I understand to remove the WordPress version tag I add:

我了解删除我添加的 WordPress 版本标签:

remove_action( 'wp_head', 'wp_generator' ); // goes into functions.php

But how do I remove the themes metatags?

但是如何删除主题meta标签?

回答by lmmendes

If you are trying only to remove the meta="generator"add this line to your functions.php.

如果您只想删除meta="generator"将这一行添加到您的functions.php

remove_action( 'wp_head', 'wp_generator' );

回答by webbernaut

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 WordPressgenerator or popular plugins like WooCommerce.

正在寻找删除图层滑块元生成器的解决方案,在我查看的少数几个网站上都没有找到太多帮助,它们都共享相同的信息,这些信息仅适用于WordPress生成器或流行的插件,如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.

这里的问题是每个插件都有自己的钩子名称和命名约定,所以学习或了解它们几乎是不可能的。我认为最简单的方法是带有preg_replace.

Working code that has been tested in WordPress 4.7.2. Inside functions.php of your theme drop in this code and it should work.

已在WordPress 4.7.2 中测试过的工作代码。在您的主题的functions.php 中放入此代码,它应该可以工作。

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

I am using regular expression to capture the metatag. It covers whether they put spaces in between the equals sign or not. Using ob_startto cover the whole document. So we add the preg_replacestarting at the header all the way to the footer. See how ob_startworks in the PHP Manual, also there are times WordPress codexdoes states it should be using ob_start.

我正在使用正则表达式来捕获meta标签。它涵盖了他们是否在等号之间放置空格。利用ob_start覆盖整个文件。所以我们preg_replace从页眉开始一直添加到页脚。查看PHP 手册ob_start中的工作原理,有时WordPress codex确实声明它应该使用。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.

我还添加了一个插件,可以在WordPress 存储库上执行此操作。

Remove Meta Generators

删除元生成器

回答by IvanRF

At the bottom of the functions.phpfile add this following php snippet:

functions.php文件的底部添加以下php片段:

// hide the meta tag generator from head and rss
function disable_version() {
   return '';
}
add_filter('the_generator','disable_version');
remove_action('wp_head', 'wp_generator');

回答by user850010

I found thissource code of a plugin which states that it removes the auto-generated WP meta tags. You could try that.

我发现其中指出,它删除自动生成的WP meta标签插件的源代码。你可以试试。

  • Plugin Name: Remove WP Meta
  • Plugin URI: http://leekelleher.com/
  • Description: This plugin removes the auto-generated WP meta tags from each webpage.
  • Author: Lee Kelleher
  • 插件名称:Remove WP Meta
  • 插件URI:http: //leekelleher.com/
  • 说明:此插件从每个网页中删除自动生成的 WP 元标记。
  • 作者:李凯莱赫

回答by T.Todua

NO!

不!

In case if it is hardcoded into your theme's template (i.e. in header.php),then you have to manually remove that!

如果它被硬编码到您的主题模板中(即在 header.php 中),那么您必须手动删除它!

Otherwise, use this full solution, to remove all version tags:

否则,请使用此完整解决方案来删除所有版本标签:

// ============ removing inbuilt WP meta-tag ===========  http://stackoverflow.com/q/16335347/2377343  ========== //
  //if included in wp_head
    add_action( 'after_setup_theme', 'my_wp_version_remover' ); function my_wp_version_remover(){
        remove_action('wp_head', 'wp_generator'); 
    }
  //clean all responses from VERSION GENERATOR
    add_filter('the_generator',             'rm_generator_filter'); 
    add_filter('get_the_generator_html',    'rm_generator_filter');
    add_filter('get_the_generator_xhtml',   'rm_generator_filter');
    add_filter('get_the_generator_atom',    'rm_generator_filter');
    add_filter('get_the_generator_rss2',    'rm_generator_filter');
    add_filter('get_the_generator_comment', 'rm_generator_filter');
    add_filter('get_the_generator_export',  'rm_generator_filter');
    add_filter('wf_disable_generator_tags', 'rm_generator_filter');
                                    function rm_generator_filter() {return '';}
// ========================================================== //

回答by u476945

I recently ran into this issue, and had to remove the meta tags for security and spam reasons for a client. I was able to remove Wordpress's meta generator but the theme uses woo framework so using

我最近遇到了这个问题,出于安全和垃圾邮件的原因,我不得不为客户端删除元标记。我能够删除 Wordpress 的元生成器,但主题使用 woo 框架,因此使用

remove_action('wp_head', 'wp_generator');

Is not sufficient. To remove

是不够的。去除

<meta name="generator" content="Woo Framework Version x.x.x" />

and anything any meta generator tags that your theme generates simply add this line to the end of your template's functions.php

以及您的主题生成的任何元生成器标签,只需将此行添加到模板的functions.php 的末尾

// remove the unwanted <meta> links
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'woo_version'); 

This worked for me on Woo Framework 5.5.5. To locate where the generator meta tag is initialized, look for your admin-init.phpfile for your template and woo_version()function and woo_version_init()function should be there. Usually its under the includes folder within your theme source.

这在 Woo Framework 5.5.5 上对我有用。要找到初始化生成器元标记的位置,请查找admin-init.php模板和woo_version()函数的文件,并且woo_version_init()函数应该在那里。通常它在主题源中的包含文件夹下。

回答by Helge Klein

The following code gets rid of all generator tags in the Woo Framework. I have tested it with Woo Framework 6.0.4 and the theme Canvas 5.8.3:

下面的代码去掉了 Woo 框架中的所有生成器标签。我已经使用 Woo Framework 6.0.4 和主题 Canvas 5.8.3 对其进行了测试:

// Remove the WooThemes version from the html headers
function no_woo_version ()
{
   return true;
}
add_filter ('wf_disable_generator_tags', 'no_woo_version');

回答by Maulik Kotak

Here is the plugin which will remove WP meta and version removal: https://wordpress.org/plugins/wp-meta-and-version-remover/

这是将删除 WP 元和版本删除的插件:https: //wordpress.org/plugins/wp-meta-and-version-remover/

回答by Александр Сонич

If you made your ouw custom WordPress theme, there will be no problem with generator in meta, DO like i Did in samll sample. There will be no generator, if you will not declare it like some of your custom functions. I try to control all JS scripts and Styles of my theme like here. If i have, style from plugins, there some more Job needed.

如果您制作了 ouw 自定义 WordPress 主题,那么元中的生成器就没有问题,就像我在 samll 示例中所做的那样。如果您不像某些自定义函数那样声明它,则不会有生成器。我尝试像这里一样控制我的主题的所有 JS 脚本和样式。如果我有插件样式,则需要更多工作。

but if you use, free theme, yes in 100% there will be generator. So add in File Function.php 1: http://sierra-group.in.ua/start-legkogo-rezhima-preprocesornoj-sborki-vashih-fajlov-stilej-i-skriptov.html/#custom-register-styles-sctiprs

但是如果你使用免费主题,是的,100% 会有发电机。所以添加文件 Function.php 1http: //sierra-group.in.ua/start-legkogo-rezhima-preprocesornoj-sborki-vashih-fajlov-stilej-i-skriptov.html/#custom-register-styles-节拍器

function disable_version() { return '';}
add_filter('the_generator','disable_version');
remove_action( 'wp_head', 'wp_generator');
remove_action('wp_head', 'woo_version'); 
function no_woo_version (){   return true;}
add_filter ('wf_disable_generator_tags', 'no_woo_version');