我对这些线得到了相同的结果,那么apply_filters
做
Line 1 : echo $instance[\'title\'];
Line 2 : echo apply_filters(\'widget_title\', $instance[\'title\']);
我对这些线得到了相同的结果,那么apply_filters
做
Line 1 : echo $instance[\'title\'];
Line 2 : echo apply_filters(\'widget_title\', $instance[\'title\']);
好apply_filter
创建可在整个系统中动态使用的全局筛选器挂钩。这使您能够过滤$instance[\'title\']
, 意味着您可以超越内容,也可以修改内容。下面是一个例子-
add_filter( \'widget_title\', \'widget_title_to_uppercase\', 10, 1);
function widget_title_to_uppercase( $content ){
$content = strtoupper($content);
return $content;
}
现在,所有小部件标题都将是大写的。因此,使用此过滤器挂钩,您可以过滤widget_title
从WordPress系统的任何地方。但在$instance[\'title\']
这是不可能的。如果尚未使用编写筛选器add_filter
显然,如果应用过滤器,什么也不会发生。
在你的functions.php
例如:
add_filter( \'widget_title\', \'wpse236433_invert_string\' );
function wpse236433_invert_string ($title) {
return strrev($title);
}
apply_filters()
允许您modify 使用挂钩的值。让我解释一下-
Sample-1: 考虑以下代码,它将显示Hello World!,
$str = \'Hello World!\';
echo $str;
Sample-2: 将显示以下代码Hello World! 也是,但它将使字符串可修改-$str = \'Hello World!\';
echo apply_filters( \'modify_str\', $str );
Sample-3: 如果使用过滤器挂钩(在插件或functions.php文件中),我在示例2中编写的代码将显示HELLO WORLD!function modify_hello_world( $str ){
return strtoupper( $str );
}
add_filter( \'modify_str\', \'modify_hello_world\' );
希望它有意义。可以安全地编辑wp内容/语言/插件/woocommerce-ro\\U ro中的语言文件。mo(.po)?我这样问是因为我不想在插件更新时覆盖它们。抱歉,如果是愚蠢的问题。