Design System is here - Read the release post

Learn GeneratePress

Developers

Conditions: registering types and rules (Pro)

Hooks for adding condition types, changing the rules and metadata the editor shows, and controlling who can use conditions. To build a condition type, see Registering conditions. To change the rules of a built-in type, see Conditions: built-in rule filters.

Jump to

Registering

generateblocks_register_conditions

GB Pro

The generateblocks_register_conditions action fires on init at priority 10, after the built-in condition types are registered at priority 0. Register your own condition types here.

Parameters: none. Default: —

add_action( 'generateblocks_register_conditions', function() {
    GenerateBlocks_Pro_Conditions_Registry::register(
        'my_type',
        [
            'label'     => __( 'My condition', 'my-plugin' ),
            'operators' => [ 'is', 'is_not' ],
            'priority'  => 70,
        ],
        'My_Plugin_Condition'
    );
} );

Related: Registering conditions Guide: Conditions

Back to top

Types, rules and metadata

generateblocks_condition_types

GB Pro

The generateblocks_condition_types filter changes the list of condition types, for example to rename one.

Parameters: $types (array keyed by type, each with label, class and operators). Default: all registered types, sorted by priority. The priority value isn’t included.

add_filter( 'generateblocks_condition_types', function( $types ) {
    if ( isset( $types['device'] ) ) {
        $types['device']['label'] = __( 'Device type', 'my-plugin' );
    }

    return $types;
} );

Related: generateblocks_condition_rules Guide: Conditions

generateblocks_condition_rules

GB Pro

The generateblocks_condition_rules filter changes the rules of any condition type. It runs after the type’s own rules filter, such as generateblocks_post_meta_rules.

Parameters: $rules (array of rule key => label), $type (string, the condition type). Default: the rules returned by the type’s get_rules().

add_filter( 'generateblocks_condition_rules', function( $rules, $type ) {
    if ( 'post_meta' === $type ) {
        $rules['my_meta_key'] = __( 'My meta key', 'my-plugin' );
    }

    return $rules;
}, 10, 2 );

Related: Conditions: built-in rule filters Guide: Conditions

generateblocks_rule_metadata

GB Pro

The generateblocks_rule_metadata filter changes the metadata the editor uses to show a rule’s value field.

Parameters: $metadata (array), $type (string, the condition type), $rule (string, the rule key). Default: the metadata from the type’s get_rule_metadata(). Unknown types get needs_value true, value_type text and supports_multi false.

The metadata keys are needs_value (bool), value_type and supports_multi (bool). The value_type values used by the built-in types are none, text, number, datetime, time, day_selector, custom_field, object_selector and hierarchical_object_selector.

add_filter( 'generateblocks_rule_metadata', function( $metadata, $type, $rule ) {
    if ( 'post_meta' === $type && 'my_meta_key' === $rule ) {
        $metadata['needs_value'] = true;
        $metadata['value_type']  = 'number';
    }

    return $metadata;
}, 10, 3 );

Related: Registering conditions Guide: Conditions

Back to top

Permissions and limits

generateblocks_conditions_capability

GB Pro — since GenerateBlocks 2.4.

The generateblocks_conditions_capability filter sets the capability needed to use or manage conditions.

Parameters: $capability (string), $context (string: use to select existing conditions, or manage to create, edit and open the dashboard). Default: edit_posts for use, manage_options for manage.

add_filter( 'generateblocks_conditions_capability', function( $capability, $context ) {
    if ( 'manage' === $context ) {
        return 'edit_others_posts';
    }

    return $capability;
}, 10, 2 );

Related: generateblocks_overlays_capability Guide: Conditions

generateblocks_condition_usage_handlers

GB Pro

The generateblocks_condition_usage_handlers filter changes the places where the conditions dashboard looks for a condition’s usage. The only handler in this list is overlay_display_conditions, which searches overlay panel display conditions.

Parameters: $handlers (array keyed by handler ID, each with method and label), $condition_id (int). Default: one handler, overlay_display_conditions.

A handler’s method must be the name of a method that already exists on GenerateBlocks_Pro_Conditions_REST. Handlers whose method doesn’t exist are skipped, so this filter is mainly for removing or relabelling handlers.

add_filter( 'generateblocks_condition_usage_handlers', function( $handlers, $condition_id ) {
    unset( $handlers['overlay_display_conditions'] );

    return $handlers;
}, 10, 2 );

Related: generateblocks_usage_search_post_types Guide: Conditions

generateblocks_menu_conditions_limit

GB Pro

The generateblocks_menu_conditions_limit filter sets how many conditions the menu item conditions control loads at once.

Parameters: $limit (int). Default: 100.

add_filter( 'generateblocks_menu_conditions_limit', function( $limit ) {
    return 200;
} );

Related: generateblocks_conditions_capability Guide: Menu Conditions

Back to top