如何从 Yoast SEO 插件中删除操作
How to remove actions from Yoast SEO plugin
我需要删除 Yoast SEO 添加的操作。这是我的代码:
function remove_actions() {
// deregister all not more required tags
remove_action( 'wp_head', '_wp_render_title_tag', 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'test123' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'front_page_specific_init' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metadesc' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'robots' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metakeywords' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'canonical' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'adjacent_rel_links' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'publisher' ), 50 );
}
add_action( 'wp_head', 'remove_actions', 1000 );
此代码不会删除操作。怎么了?如何成功删除操作?
参考 remove_action 文档中的这些注释:
- You may need to prioritize the removal of the action to a hook that occurs after the action is added.
- You cannot successfully remove the action before it has been added.
- You also cannot remove an action after it has been run.
- To remove an action the priority must match the priority with with the function was originally added.
在你的情况下,我认为其中的几个问题(尤其是#3 和#4)导致了问题:
首先,您 add_action
的优先级太高了。通过将它设置得这么高,运行宁 在 所有 Yoast wp_head
操作都是 运行 之后。相反,挂钩到您要删除的相同操作,但使用非常 低 的数字,例如 -99999,使其在 运行 之前 Yoast 操作是 运行。 (此外,我已经分成两个函数,只是为了 确保 它们在正确的时间 运行 - 每个动作一个 - wp_head
和 wpseo_head
).
其次,您的优先级与 Yoast 代码中的优先级不匹配。我已经挖掘了所有 Yoast 代码以找到所有这些操作并在下面的代码中记录/更正 - 我可以告诉你例如 Yoast 代码中的 metakeywords
钩子是 11,所以你的 remove_action(优先级为 40)将不起作用。
最后,Yoast 将这些操作添加到 $this
(WPSEO_Frontend class 的实例化版本),而不是 class 方法的静态版本。这意味着 remove_action
无法根据函数数组(WPSEO_Frontend
、head
)找到它们。相反,您需要加载 Yoast 的实例化版本,并将 that 传递给 remove_action
函数。
记录在下面的代码:
// Remove ONLY the head actions. Permits calling this at a "safe" time
function remove_head_actions() {
// not Yoast, but WP default. Priority is 1
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
// If the plugin isn't installed, don't run this!
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the WPSEO_Frontend instantiated class
$yoast = WPSEO_Frontend::get_instance();
// removed your "test" action - no need
// per Yoast code, this is priority 0
remove_action( 'wp_head', array( $yoast, 'front_page_specific_init' ), 0 );
// per Yoast code, this is priority 1
remove_action( 'wp_head', array( $yoast, 'head' ), 1 );
}
function remove_wpseo_head_actions() {
// If the Yoast plugin isn't installed, don't run this
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the Yoast instantiated class
$yoast = WPSEO_Frontend::get_instance();
remove_action( 'wpseo_head', array( $yoast, 'head' ), 50 );
// per Yoast code, this is priority 6
remove_action( 'wpseo_head', array( $yoast, 'metadesc' ), 6 );
// per Yoast code, this is priority 10
remove_action( 'wpseo_head', array( $yoast, 'robots' ), 10 );
// per Yoast code, this is priority 11
remove_action( 'wpseo_head', array( $yoast, 'metakeywords' ), 11 );
// per Yoast code, this is priority 20
remove_action( 'wpseo_head', array( $yoast, 'canonical' ), 20 );
// per Yoast code, this is priority 21
remove_action( 'wpseo_head', array( $yoast, 'adjacent_rel_links' ), 21 );
// per Yoast code, this is priority 22
remove_action( 'wpseo_head', array( $yoast, 'publisher' ), 22 );
}
最后的注释:。
删除 WPSEO_Frontend::head 操作非常严厉。这将删除一大堆您可能不想删除的其他内容。
其次,修改这些操作的输出可能更好,而不是完全删除它们。
例如,
add_action('wpseo_metakeywords', 'your_metakeywords_function');
function your_metakeywords_function( $keywords ) {
// modify the keywords as desired
return $keywords;
}
其中许多操作都有过滤器,可以通过返回 false
来删除输出。
// Removes 'meta name="description"' tag from output
add_filter( 'wpseo_metadesc', 'my_custom_metadesc' );
function my_custom_metadesc() {
return false;
}
在某些情况下,例如 WPSEO_Opengraph,有一个过滤模式:wpseo_og_ + 属性 名称用下划线代替冒号。
// Filters '<meta property="article:tag" content="Foo" />'
add_filter( 'wpseo_og_article_tag', 'my_custom_article_tag' );
我需要删除 Yoast SEO 添加的操作。这是我的代码:
function remove_actions() {
// deregister all not more required tags
remove_action( 'wp_head', '_wp_render_title_tag', 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'test123' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'front_page_specific_init' ), 50 );
remove_action( 'wp_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'head' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metadesc' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'robots' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'metakeywords' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'canonical' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'adjacent_rel_links' ), 50 );
remove_action( 'wpseo_head', array( 'WPSEO_Frontend', 'publisher' ), 50 );
}
add_action( 'wp_head', 'remove_actions', 1000 );
此代码不会删除操作。怎么了?如何成功删除操作?
参考 remove_action 文档中的这些注释:
- You may need to prioritize the removal of the action to a hook that occurs after the action is added.
- You cannot successfully remove the action before it has been added.
- You also cannot remove an action after it has been run.
- To remove an action the priority must match the priority with with the function was originally added.
在你的情况下,我认为其中的几个问题(尤其是#3 和#4)导致了问题:
首先,您 add_action
的优先级太高了。通过将它设置得这么高,运行宁 在 所有 Yoast wp_head
操作都是 运行 之后。相反,挂钩到您要删除的相同操作,但使用非常 低 的数字,例如 -99999,使其在 运行 之前 Yoast 操作是 运行。 (此外,我已经分成两个函数,只是为了 确保 它们在正确的时间 运行 - 每个动作一个 - wp_head
和 wpseo_head
).
其次,您的优先级与 Yoast 代码中的优先级不匹配。我已经挖掘了所有 Yoast 代码以找到所有这些操作并在下面的代码中记录/更正 - 我可以告诉你例如 Yoast 代码中的 metakeywords
钩子是 11,所以你的 remove_action(优先级为 40)将不起作用。
最后,Yoast 将这些操作添加到 $this
(WPSEO_Frontend class 的实例化版本),而不是 class 方法的静态版本。这意味着 remove_action
无法根据函数数组(WPSEO_Frontend
、head
)找到它们。相反,您需要加载 Yoast 的实例化版本,并将 that 传递给 remove_action
函数。
记录在下面的代码:
// Remove ONLY the head actions. Permits calling this at a "safe" time
function remove_head_actions() {
// not Yoast, but WP default. Priority is 1
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
// If the plugin isn't installed, don't run this!
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the WPSEO_Frontend instantiated class
$yoast = WPSEO_Frontend::get_instance();
// removed your "test" action - no need
// per Yoast code, this is priority 0
remove_action( 'wp_head', array( $yoast, 'front_page_specific_init' ), 0 );
// per Yoast code, this is priority 1
remove_action( 'wp_head', array( $yoast, 'head' ), 1 );
}
function remove_wpseo_head_actions() {
// If the Yoast plugin isn't installed, don't run this
if ( ! is_callable( array( 'WPSEO_Frontend', 'get_instance' ) ) ) {
return;
}
// Get the Yoast instantiated class
$yoast = WPSEO_Frontend::get_instance();
remove_action( 'wpseo_head', array( $yoast, 'head' ), 50 );
// per Yoast code, this is priority 6
remove_action( 'wpseo_head', array( $yoast, 'metadesc' ), 6 );
// per Yoast code, this is priority 10
remove_action( 'wpseo_head', array( $yoast, 'robots' ), 10 );
// per Yoast code, this is priority 11
remove_action( 'wpseo_head', array( $yoast, 'metakeywords' ), 11 );
// per Yoast code, this is priority 20
remove_action( 'wpseo_head', array( $yoast, 'canonical' ), 20 );
// per Yoast code, this is priority 21
remove_action( 'wpseo_head', array( $yoast, 'adjacent_rel_links' ), 21 );
// per Yoast code, this is priority 22
remove_action( 'wpseo_head', array( $yoast, 'publisher' ), 22 );
}
最后的注释:。
删除 WPSEO_Frontend::head 操作非常严厉。这将删除一大堆您可能不想删除的其他内容。
其次,修改这些操作的输出可能更好,而不是完全删除它们。
例如,
add_action('wpseo_metakeywords', 'your_metakeywords_function');
function your_metakeywords_function( $keywords ) {
// modify the keywords as desired
return $keywords;
}
其中许多操作都有过滤器,可以通过返回 false
来删除输出。
// Removes 'meta name="description"' tag from output
add_filter( 'wpseo_metadesc', 'my_custom_metadesc' );
function my_custom_metadesc() {
return false;
}
在某些情况下,例如 WPSEO_Opengraph,有一个过滤模式:wpseo_og_ + 属性 名称用下划线代替冒号。
// Filters '<meta property="article:tag" content="Foo" />'
add_filter( 'wpseo_og_article_tag', 'my_custom_article_tag' );