NO BUG: Bricks Conditions not executing callbacks

I wrote the following custom conditions:

add_filter('bricks/conditions/groups', function ($groups) {
    $groups[] = [
        'name'  => 'custom_functions',
        'label' => esc_html__('Custom Functions', 'my-plugin'),
    ];
    return $groups;
});

add_filter('bricks/conditions/options', function ($options) {
    error_log('[CHECK] Registering Bricks condition');

    $options[] = [
        'key'   => 'has_related_products',
        'label' => esc_html__('Has Related Products', 'my-plugin'),
        'group' => 'custom_functions',
        'compare' => [
            'type' => 'select',
            'options' => [
                '==' => esc_html__('is', 'my-plugin'),
                '!=' => esc_html__('is not', 'my-plugin'),
            ],
            'placeholder' => esc_html__('is', 'my-plugin'),
        ],
        'value' => [
            'type' => 'select',
            'options' => [
                'true'  => esc_html__('True', 'my-plugin'),
                'false' => esc_html__('False', 'my-plugin'),
            ],
        ],
		'callback' => function($compare, $value) {
			$product_id = get_the_ID();

			error_log('[DEBUG] Bricks condition evaluating...');
			error_log('[DEBUG] Current post ID: ' . $product_id);

			$related = wc_get_related_products($product_id);
			error_log('[DEBUG] Related count: ' . count($related));
			error_log('[DEBUG] Related IDs: ' . implode(', ', $related));

			// FORCE SHOW FOR DEBUGGING
			return true;
		},
    ];

    return $options;
});

add_filter('bricks/conditions/groups', function ($groups) {
    $groups[] = [
        'name'  => 'custom_debug',
        'label' => esc_html__('Debug Group', 'my-plugin'),
    ];
    return $groups;
});

add_filter('bricks/conditions/options', function ($options) {
    $options[] = [
        'key'   => 'force_always_true',
        'label' => esc_html__('Force True (Debug)', 'my-plugin'),
        'group' => 'custom_debug',
        'compare' => [
            'type' => 'select',
            'options' => [
                '==' => esc_html__('is', 'my-plugin'),
            ],
        ],
        'value' => [
            'type' => 'select',
            'options' => [
                'true' => esc_html__('True', 'my-plugin'),
            ],
        ],
        'callback' => function ($compare, $value) {
            error_log('[DEBUG] Bricks forced debug condition executed');
            return true;
        },
    ];
    return $options;
});

the conditions get registered (debug.log) but the callbacks are never being executed. I tried it in different scenarios. Could this be an execution bug?

Hi @stellardynamix and welcome to the community :partying_face:

This is not a bug - if you follow the bricks academy article (Element Conditions – Bricks Academy) , you will notice that you need three things:

  1. bricks/conditions/groups, which you have
  2. bricks/conditions/options, which you also have
  3. bricks/conditions/result, which is missing and is a mandatory, as here you will compare return the results.

If you copy-paste the exaple from the academy, it should work.

Let me know if you need more help with that.
Matej

1 Like

Hi Matej,
thanks for offering your help. I tried adjusting the code, but for some reason I get the same outcome:

add_action('acf/init', function () {
    // Register custom condition group
    add_filter('bricks/conditions/groups', function ($groups) {
        $groups[] = [
            'name'  => 'custom_functions',
            'label' => esc_html__('Custom Functions', 'your-child-theme'),
        ];
        return $groups;
    });

    // Register condition options
    add_filter('bricks/conditions/options', function ($options) {
        foreach (['header_notice_active' => 'Header Notice is active', 'footer_notice_active' => 'Footer Notice is active'] as $key => $label) {
            $options[] = [
                'key'   => $key,
                'label' => esc_html__($label, 'your-child-theme'),
                'group' => 'custom_functions',
                'compare' => [
                    'type'    => 'select',
                    'options' => [
                        '==' => esc_html__('is', 'your-child-theme'),
                        '!=' => esc_html__('is not', 'your-child-theme'),
                    ],
                    'placeholder' => esc_html__('Select', 'your-child-theme'),
                ],
                'value' => [
                    'type'    => 'select',
                    'options' => [
                        'true'  => esc_html__('True', 'your-child-theme'),
                        'false' => esc_html__('False', 'your-child-theme'),
                    ],
                ],
            ];
        }
        return $options;
    });

    // Evaluate condition
add_filter('bricks/conditions/result', function ($result, $condition_key, $condition) {
    if (!in_array($condition_key, ['header_notice_active', 'footer_notice_active'])) {
        return $result;
    }

    $compare = $condition['compare'] ?? '==';
    $value   = $condition['value'] ?? 'false';

    $acf_field_key = $condition_key === 'header_notice_active' ? 'header_notice' : 'footer_notice';
    $field_content = get_field($acf_field_key, '950'); // <- using post ID 950 explicitly

    // Treat any non-empty string as "active"
    $is_active = !empty(trim($field_content));
    $expected = $value === 'true';

    return $compare === '==' ? $is_active === $expected : $is_active !== $expected;
}, 10, 3);
});

with:

add_action('init', function () {
    $value = get_field('header_notice', '950');
    error_log('Header Notice: ' . var_export($value, true));
});

I did check the output of this variable and it correctly displays the text I have saved in this ACF field. so if text is entered in this field, it should display the section, otherwise not. But for some reason it still doesnt and seems to return false

Ah! I got it working… i used the wrong function call (the old function) and now it works flawlessy! thank you!

1 Like

Perfect, I’m happy that it’s working now and that it’s solved :slight_smile:

Best regards,
Matej