2017-02-13 26 views
1

我正在努力完成一项艰巨的任务。任务是使用可用的WP钩子从Yoast SEO文件中删除echo删除函数的回显 - WordPress

类frontend.php

public function head() { 
    global $wp_query; 

    $old_wp_query = null; 

    if (! $wp_query->is_main_query()) { 
     $old_wp_query = $wp_query; 
     wp_reset_query(); 
    } 

    /** 
    * Action: 'wpseo_head' - Allow other plugins to output inside the Yoast SEO section of the head section. 
    */ 
    do_action('wpseo_head'); 

    echo '<!--/', $this->head_product_name(), ". -->\n\n"; // <-- remove this 

    if (! empty($old_wp_query)) { 
     $GLOBALS['wp_query'] = $old_wp_query; 
     unset($old_wp_query); 
    } 

    return; 
} 

有没有办法超越此功能,并使用现有的WP钩去掉echo?还是有更好的方法来做到这一点?

非常感谢。

+0

鉴于echo命令不在条件执行路径中,显然没有办法调用“hook”可以避免它被调用(除了“hook”完全替换了head()函数共)。 – eggyal

+0

@eggyal你知道用我自己的方式替换这个函数吗? – Mitch

+1

你能解释一下如何消除这条线让你的生活变得更好吗?可能有其他选择。 – RST

回答

0

is a post at wordpress.stackexchange,表示可以使用钩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('<!--/Yoast WordPress SEO plugin. -->', '', $output); 
    } 
    return $output; 
} 

那等着您$this->head_product_name()/ Yoast WordPress SEO plugin.,所以你可能要改变这种...或者,如果你希望它去掉“anythere”在那里,您可以更换改变这样的事情:

$output = preg_replace('/<!-- .* -->/Us', '', $output); 

但是,将删除所有的意见,这将是比慢〜应变克替换。

+0

这会起作用,但是没有任何东西可以覆盖Yoast类中的head()函数? – Mitch

+0

你的请求是使用可用的钩子并删除回声。这是做的。你需要一个新的问题。 – WEBjuju