[英]Remove echo from function - WordPress
我正在努力完成一項艱巨的任務。 任務是使用可用的WP鈎子從Yoast SEO文件中刪除echo
。
類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
? 或者有更好的方法嗎?
非常感謝。
在wordpress.stackexchange上有一個帖子 ,表明你可以使用鈎子get_header
和wp_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);
但是這將刪除所有注釋,並且它將比字符串替換慢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.