如果插件的一个过滤器返回true
。
我有一个过滤器:
function test_filter() {
$is_enabled = true;
$is_enabled = apply_filters( 'test_filter', $is_enabled );
return $is_enabled;
}
我有一个我想需要的课:
require_once( PATH . 'class.php' );
我想知道是否有一种方法可以根据test_filter()
过滤器有条件地包含此类。我尝试了:
if( test_filter() === true ) {
require_once( PATH . 'class.php' );
}
我想,由于if
语句在过滤器之前发射,因此无法正常工作。任何洞察力或反馈都将不胜感激!
在评论中发布得太多了,希望这能使您朝正确的方向发展..
每个注释,您是正确的:插件已经在主题中的任何挂钩/过滤器之前已加载可以运行。
如果您参考WordPress操作参考,您将在加载主题之前看到插件已加载。
您可能会尝试的是将放入钩子内,以确保在加载此之前已经加载主题。
类似的东西:
// first hook that fires after theme is loaded.
// you may also want to consider the 'init' action
add_action( 'after_setup_theme', 'include_my_file' );
// obviously name this something a bit better :)
function include_my_file() {
// switched to Yoda-style for better "defensiveness"
if( TRUE === test_filter() ) {
require_once( PATH . 'class.php' );
}
}
// your original function
function test_filter() {
$is_enabled = true;
$is_enabled = apply_filters( 'test_filter', $is_enabled );
return $is_enabled;
}
请注意,这实际上可以简化一个合理的数量:
add_action( 'after_setup_theme', 'include_my_file' );
function include_my_file() {
if( TRUE === apply_filters( 'test_filter', TRUE ) ) {
require_once( PATH . 'class.php' );
}
}