Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Exposing Element Controls for New Conditions

  • I have been running this to use Element Controls for display conditions. Has worked well for years.

    // Generate custom taxonomy for Element Display Rules
    function element_control_custom_tax() {
        register_taxonomy(
            'element-controls',
            array( 'page' ),
            array(
                'label' => __( 'Element Controls' ),
                'rewrite' => array( 'slug' => 'element-controls' ),
                'hierarchical' => true,
                'show_in_rest' => true,
            )
        );
    }
    
    add_action( 'init', 'element_control_custom_tax' );

    Noticed that this is not exposed in the new conditions.

    This snippet works but I would feel more comfortable getting someone with deeper knowledge of GP + GB to take a look. Can someone make this fit the newer GP environment and update it to give prepopulated dropdowns like in Elements? Don’t want to break anything either:

    
    /**
     * Expose 'element-controls' taxonomy as a GenerateBlocks Pro Condition.
     * Requires GenerateBlocks Pro 2.3+
     */
    
    // -------------------------------------------------------
    // 1. Condition Class
    // -------------------------------------------------------
    if ( class_exists( 'GenerateBlocks_Pro_Condition_Abstract' ) ) {
    
        class Element_Controls_Condition extends GenerateBlocks_Pro_Condition_Abstract {
    
            public function evaluate( $rule, $operator, $value, $context = [] ) {
                $post_id = $context['post_id'] ?? get_the_ID();
    
                if ( ! $post_id ) {
                    return false;
                }
    
                // Get all term slugs assigned to this post for our taxonomy
                $terms = get_the_terms( $post_id, 'element-controls' );
    
                if ( is_wp_error( $terms ) || empty( $terms ) ) {
                    $assigned = [];
                } else {
                    $assigned = wp_list_pluck( $terms, 'slug' );
                }
    
                // $value can be a single slug or array of slugs
                $check = is_array( $value ) ? $value : [ $value ];
    
                $has_match = ! empty( array_intersect( $assigned, $check ) );
    
                switch ( $operator ) {
                    case 'is':
                        return $has_match;
                    case 'is_not':
                        return ! $has_match;
                    default:
                        return false;
                }
            }
    
            public function get_rules() {
                return [
                    'has_term' => __( 'Has Element Control Term', 'your-textdomain' ),
                ];
            }
    
            public function get_rule_metadata( $rule ) {
                return [
                    'needs_value'    => true,
                    'value_type'     => 'search', // triggers the search UI
                    'supports_multi' => true,
                    'search_route'   => 'generateblocks-pro/advanced-conditions/v1/search_element_controls',
                ];
            }
    
            public function sanitize_value( $value, $rule ) {
                if ( is_array( $value ) ) {
                    return array_map( 'sanitize_text_field', $value );
                }
                return sanitize_text_field( $value );
            }
        }
    
    }
    
    // -------------------------------------------------------
    // 2. Register the Condition
    // -------------------------------------------------------
    add_action( 'generateblocks_register_conditions', function() {
        if ( ! class_exists( 'GenerateBlocks_Pro_Conditions_Registry' ) ) {
            return;
        }
    
        GenerateBlocks_Pro_Conditions_Registry::register(
            'element_controls',
            [
                'label'     => __( 'Element Controls', 'your-textdomain' ),
                'operators' => [ 'is', 'is_not' ],
            ],
            'Element_Controls_Condition'
        );
    } );
    
    // -------------------------------------------------------
    // 3. REST Endpoint for term search (powers the Conditions UI)
    // -------------------------------------------------------
    add_action( 'rest_api_init', function() {
        register_rest_route(
            'generateblocks-pro/advanced-conditions/v1',
            '/search_element_controls/',
            [
                'methods'             => 'GET',
                'callback'            => 'element_controls_condition_search',
                'permission_callback' => function() {
                    return current_user_can( 'edit_posts' );
                },
                'args' => [
                    'search' => [
                        'required'          => false,
                        'type'              => 'string',
                        'sanitize_callback' => 'sanitize_text_field',
                    ],
                ],
            ]
        );
    } );
    
    function element_controls_condition_search( WP_REST_Request $request ) {
        $search = $request->get_param( 'search' );
    
        $terms = get_terms( [
            'taxonomy'   => 'element-controls',
            'hide_empty' => false,
            'search'     => $search,
            'number'     => 20,
        ] );
    
        if ( is_wp_error( $terms ) ) {
            return new WP_REST_Response( [ 'success' => false, 'response' => [] ], 200 );
        }
    
        $results = array_map( function( $term ) {
            return [
                'value' => $term->slug,
                'label' => $term->name,
            ];
        }, $terms );
    
        return new WP_REST_Response( [ 'success' => true, 'response' => $results ], 200 );
    }
  • Hi there,

    Your second snippet isn’t required. The Location condition automatically detects all public taxonomies and exposes them in searchable dropdowns.

    What you actually need is simply your original taxonomy registration, with public => true explicitly defined for clarity:

    function element_control_custom_tax() {
        register_taxonomy(
            'element-controls',
            array( 'page' ),
            array(
                'label'        => __( 'Element Controls' ),
                'rewrite'      => array( 'slug' => 'element-controls' ),
                'hierarchical' => true,
                'public'       => true,
                'show_in_rest' => true,
            )
        );
    }
    add_action( 'init', 'element_control_custom_tax' );

    Once registered this way, the taxonomy should automatically appear in the Location condition dropdown.

  • Thanks Alvind, I have loaded the new snippet.

    Would Elemental Control be within one of these. Here is the dropdown for Conditional Group Logic:

    Select Type:
    Author
    Cookie
    Date & Time
    Device
    Language
    Location
    Post Meta
    Query Parameter
    Referrer
    Site Options
    User Meta
    User Role

  • Wait, I see it’s under Location now. The snippet targets Page but I see Post Element Controls and not Page Element Control like inside GP Elements. Still good?

  • The snippet targets Page but I see Post Element Controls and not Page Element Control like inside GP Elements.

    That’s expected behavior. The Location condition labels all taxonomy term rules as “Post {Taxonomy Name}”, regardless of which post types the taxonomy is attached to. In this context, Post doesn’t refer specifically to the Post post type, it means the content currently being viewed.

    So “Post Element Controls” will still correctly check whether the current page has the specified element-controls terms assigned.

    GP Elements labels it “Page Element Control” because its UI is aware of the attached object types. The GB Pro Conditions system uses a more generic naming convention. The functionality is the same, only the label differs.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.