[英]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文檔中的以下說明:
- 您可能需要優先考慮將操作刪除到添加操作之后發生的掛鈎上。
- 您無法在添加操作之前成功刪除該操作。
- 運行動作后,您也無法刪除它。
- 要刪除操作,優先級必須與優先級與最初添加的功能相匹配。
以您的情況,我認為其中一些問題(尤其是#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類的實例化版本)中,而不是類方法的靜態版本中。 例如,這意味着remove_action
無法基於函數數組( WPSEO_Frontend
, head
)找到它們。 相反,你需要加載Yoast的實例化版本,並傳遞到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' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.