实现这一点的一种简单方法(但不使用类方法)是过滤wp_head 使用操作挂钩output buffering.
在您的主题中header.php, 包装wp_head() 使用呼叫ob_start($cb) 和ob_end_flush(); 功能如下:
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_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(\'<!-- 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;
}