16th Jul '24
/
0 comments

JetEngine Checkbox Condition in Bricks

This tutorial shows how elements in Bricks can be conditionally output depending on the selected option from a JetEngine checkbox custom field.

Consider a meta box titled “Post Fields” enabled for Posts having a “Features” meta box that is of type Checkbox with these possible options: Room, Wifi, Air Condition.

For Post A: Room is ticked.

For Post B: Wifi is ticked.

For Post C: Air Condition is ticked.

In the single Post template, we want a Block element to be output only if the current post has Room feature ticked.

Another Block element should be output only if the post has Wifi feature ticked.

Another Block element should be output only if the post has Air Condition feature ticked.

Step 1

Register the meta box.

Step 2

Edit your posts and tick the feature(s) it should have.

Step 3

Register a custom function that takes the feature string as its input and returns true if the current post’s feature field value has it set to ‘true’ or false if otherwise.

This is how the values are stored in a JetEngine Checkbox field:

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php 

function bl_is_feature_enabled( string $feature ): bool {
    // Get the features array from post meta
    $features = get_post_meta( get_the_ID(), 'features', true );

    // Check if the feature exists in the array and if it is set to true
    if ( isset( $features[$feature] ) && $features[$feature] === 'true' ) {
        return true;
    }

    return false;
}

Step 4

Whitelist the bl_is_feature_enabled function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_is_feature_enabled'
  ];
} );

You should also add other functions (native or custom) being used in your Bricks instance besides bl_is_feature_enabled. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.

Step 5

Edit your single post template.

Create the three Blocks and apply dynamic data condition on each.

{echo:bl_is_feature_enabled(Room)}

Similarly, apply conditions on the other two blocks by passing the feature value in the parenthesis as the argument.

If the argument has a space, wrap it in single quotes like this:

{echo:bl_is_feature_enabled('Air Condition')}

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Bricks Dynamic Condition – Check if today falls within 5 days before any holiday date

Creating a custom condition that returns true or false depending on whether today falls within 5 days before any holiday dates selected on a Meta…
Categories:
Tags:
Conditional Output based on Manual Excerpt in Bricks

Conditional Output based on Manual Excerpt in Bricks

How to display a post’s excerpt only if it has been manually set.
Categories:
Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

A user asked: I have a CTA in the footer of my site, which I have custom fields on various templates, etc to populate. I…
Categories:
Pro
Query Variables Condition in Bricks

Query Variables Condition in Bricks

This Pro tutorial provides the steps to register a custom function that returns true or false depending on whether the current page URL has specific…
Conditional Output on the First Page of Archives in Bricks

Conditional Output on the First Page of Archives in Bricks

How to output an element on an archive page only if we're on the first page of the archive.
Categories:
Pro
Conditional CSS in Bricks based on Post Meta

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked: Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the…
Pro
Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

Related Episodes Grouped by Episode Chapters on Single Podcasts in Bricks when using JetEngine

A user asks: How to Display Related Episodes Grouped by Chapters on a Podcast Page in Bricks? Hi everyone, I have two post types created…
Checking for Post Type in Bricks

Checking for Post Type in Bricks

A way to output elements conditionally based on the current post type in the loop in Bricks.
Categories: