前言
筛选 Bricks 控制面板的内容元素类别。
用法
将代码加入子主题的 functions.php:
add_filter( 'bricks/conditions/result', function( $result, $condition_key, $condition ) {
// Logic for custom 'is_weekend' condition
if ( $condition_key === 'is_weekend' ) {
$is_weekend = ( date( 'N' ) >= 6 );
$value = isset( $condition['value'] ) ? $condition['value'] : 'true';
// Check if condition value matches current state
if ( $value === 'true' ) {
return $is_weekend;
} else {
return ! $is_weekend;
}
}
return $result;
}, 10, 3 );
参数
$result(bool):条件评估的结果。$condition_key(string):正在被评估的条件键(例如 user_role、my_custom_condition)。$condition(array):条件设置(包含 compare、value 等)。
示例代码
筛选返回值
add_filter( 'bricks/conditions/result', function( $result, $condition_key, $condition ) {
// Logic for custom 'is_weekend' condition
if ( $condition_key === 'is_weekend' ) {
$is_weekend = ( date( 'N' ) >= 6 );
$value = isset( $condition['value'] ) ? $condition['value'] : 'true';
// Check if condition value matches current state
if ( $value === 'true' ) {
return $is_weekend;
} else {
return ! $is_weekend;
}
}
return $result;
}, 10, 3 );
根据条件短路返回原值
根据条件跳过或修改返回值:
add_filter( 'bricks/conditions/result', function( $value, $result ) {
if ( is_admin() ) { return $value; }
// TODO: custom logic here
return $value;
} );
补充说明
更多细节可参考 官方文档。