問題描述

我正在嘗試類似於這個問題:remove_action or remove_filter with external classes?

我試圖刪除

<!-- This site is optimized with the Yoast WordPress SEO plugin v1.0.3 - http;//yoast.com/wordpress/seo/ -->

從外掛的訊息。

在你向我說明這可能是不道德的之前,作者說可以這樣做:http://wordpress.org/support/topic/plugin-wordpress-seo-by-yoast-how-to-remove-dangerous-inserted-yoast-message-in-page-headers?replies=29#post-2503475

我已經找到新增評論的類:http://plugins.svn.wordpress.org/wordpress-seo/tags/1.2.8.7/frontend/class-frontend.php

基本上,WPSEO_Frontend 類具有名為 debug_marker 的功能,然後由名為 head 的函式呼叫,然後將其新增到__Construct 中的 wp_head

我是新手上課,但我發現了一種方法,透過做完全刪除頭

global $wpseo_front;    
remove_action( 'wp_head', array($wpseo_front,'head'), 1, 1 );

但我只想從中刪除 debug_marker 部件。我試過這個,但它不工作 remove_action( 'wp_head', array($wpseo_front,'head','debug_marker'), 1, 1 );

正如我所說,我是新來的課,所以任何幫助將是巨大的。

最佳解決方案

透過使用 output buffering 過濾 wp_head 動作鉤子的輸出,實現這一點的簡單方法 (但不使用 Class 方法) 。

在您的主題 header.php 中,使用 ob_start($cb)ob_end_flush(); 函式打包 wp_head()函式,如:

ob_start('ad_filter_wp_head_output');
wp_head();
ob_end_flush();

現在在主題 functions.php 檔案中,宣告您的輸出回撥函式 (在這種情況下為 ad_filter_wp_head_output):

function ad_filter_wp_head_output($output) {
    if (defined('WPSEO_VERSION')) {
        $output = str_ireplace('<!-- This site is optimized with the Yoast WordPress SEO plugin v' . WPSEO_VERSION . ' - http://yoast.com/wordpress/seo/ -->', '', $output);
        $output = str_ireplace('<!-- / Yoast WordPress SEO plugin. -->', '', $output);
    }
    return $output;
}

如果要透過 functions.php 執行所有操作,無需編輯 header.php 檔案,您可以掛接到 get_headerwp_head 動作鉤子來定義輸出緩衝會話:

add_action('get_header', 'ad_ob_start');
add_action('wp_head', 'ad_ob_end_flush', 100);
function ad_ob_start() {
    ob_start('ad_filter_wp_head_output');
}
function ad_ob_end_flush() {
    ob_end_flush();
}
function ad_filter_wp_head_output($output) {
    if (defined('WPSEO_VERSION')) {
        $output = str_ireplace('<!-- This site is optimized with the Yoast WordPress SEO plugin v' . WPSEO_VERSION . ' - http://yoast.com/wordpress/seo/ -->', '', $output);
        $output = str_ireplace('<!-- / Yoast WordPress SEO plugin. -->', '', $output);
    }
    return $output;
}

次佳解決方案

我不認為你將能夠使用 remove_action 做到這一點。 remove_action 中的函式引數不會幫助您,因為 debug_marker()函式不是 add_action()呼叫中使用的函式。

在他的程式碼中可能有一些類似 add_action( "wp_head", "head" )的東西。因此,您可以刪除”head” 功能,但 debug_marker 沒有明確新增為一個操作。

你可以

  1. 編輯 Yoast 的原始檔並刪除除錯註釋行。

  2. 擴充套件 WPSEO_Frontend 類並過載 debug_marker 函式返回 「」 。 TBH,我不知道這將在 WP 載入外掛方面如何工作,但可能值得調查。

第三種解決方案

感謝您的幫助,我終於解決了。我為我的孩子主題建立了一個 functions.php,然後新增

// we get the instance of the class
$instance = WPSEO_Frontend::get_instance();
/* then we remove the function
    You can remove also others functions, BUT remember that when you remove an action or a filter, arguments MUST MATCH with the add_action
    In our case, we had :
    add_action( 'wpseo_head', array( $this, 'debug_marker' ), 2 );

    so we do : 
    remove_action( 'wpseo_head', array( $this, 'debug_marker' ), 2 );
    */
    remove_action( 'wpseo_head', array( $instance, 'debug_marker' ), 2 );

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。