Skip to content

Custom visibility rules ​

A custom rule appears alongside VX's built-in conditions in the Visibility rules window. This one shows or hides blocks for members of a membership plugin:

php
use VxFramework\Modules\Visibility\Context;
use VxFramework\Modules\Visibility\Rules\Base_Rule;

final class My_Is_Member_Rule extends Base_Rule {
    public static function id(): string       { return 'my_plugin:is_member'; }
    public static function label(): string    { return __( 'Visitor is a paying member', 'my-plugin' ); }
    public static function category(): string { return 'user'; }

    public static function evaluate( array $rule, Context $context ): ?bool {
        return my_membership_is_active( $context->viewer_user_id() );
    }
}

add_filter( 'vxframework/visibility/rules', function ( array $classes ) {
    $classes[] = My_Is_Member_Rule::class;
    return $classes;
} );

evaluate() returns true when the condition matches, false when it doesn't, and null when it can't be decided (the condition is then skipped).

The Context gives you the visitor (viewer_user_id(), viewer_roles()), the page (current_post_id(), current_url(), referrer(), query_param()) and the time (current_datetime(), current_weekday()), so rules don't have to read globals.

Use a prefix you own in the id. If your plugin is later removed, blocks using the rule treat it as an unknown rule type, which counts as not matching, so they behave as if the rule failed.

To change the result of any rule, VX's included, filter vxframework/visibility/rule_match ( bool $result, array $rule, Context $context ).

Technical details for LLMs
  • Base_Rule (VxFramework\Modules\Visibility\Rules): abstract static id() (<category>:<name>), label(), evaluate( array $rule, Context $context ): ?bool; optional category(), value_arity(), value_input(), operators(), descriptor() to offer an operator and value UI.
  • The stored rule is { type: <id>, operator?, value? } inside vxframeworkVisibility.groups[].rules[]. Unknown types fail closed.
  • Registered rules are listed by vx-framework/list-visibility-rules.

Documentation for the VX Framework.