首页 > 解决方案 > apply_filters 不会替换默认小部件中的文本

问题描述

我正在尝试删除 WP_Widget_Recent_Comments 在二十个主题中显示的“on”文本字符串(使用修改的字体):

在此处输入图像描述

我正在尝试在我的子主题的functions.php 中执行此操作。我发现 WP 在那里创建了这个字符串: \wp-includes\widgets\class-wp-widget-recent-comments.php 148

$output .= sprintf(
    _x( '%1$s on %2$s', 'widgets' ),

我在我的子主题 fuctions.php 中搜索并尝试了 100500 种不同的 apply_filters 变体,例如:

apply_filters('widget_text_content', 'replace_text');
function replace_text($text) {
    $text = str_replace('on','', $text);
    return $text;
}

但它们都不适用于这个小部件。同时,常规 apply_filters 到 'the_content' 过滤器工作正常。请帮忙 :)

标签: wordpress

解决方案


推荐阅读