We recognise that the Conditional Manager might not have the conditions that you’re looking for in a highly advanced setup. That’s why with these little code snippets you can implement your very own custom conditions and manage them how you see fit.
Defining Conditions
With the first snippet, you define the conditions that will be available in the Conditional Manager. It goes like this.
// Define a custom rule.
//
// Optionally, it allows to add a text field by passing `has_text_field`
// or to add a predefined list of choices by passing `choices`.
add_filter(
'blocksy:conditions:rules:custom',
function ($rules) {
$rules[] = [
'id' => 'some_custom_rule',
'title' => __('Some Custom Rule', 'blocksy-companion'),
];
$rules[] = [
'id' => 'some_custom_rule_with_text',
'title' => __('Some Custom Rule with text', 'blocksy-companion'),
// Allow user to enter a text value in the condition
'has_text_field' => true
];
$rules[] = [
'id' => 'some_custom_rule_with_choices',
'title' => __('Some Custom Rule with choices', 'blocksy-companion'),
// Allow user to select from a list of choices
'choices' => [
[
'key' => 'value1',
'value' => __('Value 1', 'blocksy-companion')
],
[
'key' => 'value2',
'value' => __('Value 2', 'blocksy-companion')
]
]
];
return $rules;
}
);
Resolving Conditions
With this snippet, you make sure the logic of the newly added conditions works as expected on the front end. It goes like this.
// Resolve the custom condition on the frontend and perform actual check against
// the potential value.
add_filter(
'blocksy:conditions:rules:resolve',
function ($result, $rule) {
if ($rule['rule'] === 'some_custom_rule_with_text') {
$value = '';
if (isset($rule['payload']['value'])) {
$value = $rule['payload']['value'];
}
// $value now has the user selected value either from choice or
// from the text field.
// Do some checks here
$my_check = true;
return $my_check;
}
return $result;
},
10,
2
);