問題描述

我正在嘗試類似於這個問題: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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。