Pass Variable through Bricks Shortcode

By WebCare in November 28, 2024 – Reading time 3 minute

The original question was posted in Bricks Forum

pass variable to bricks question

How to Pass Variable through Bricks Short Code?

The idea is to be able to reuse the same shortcode in multiple areas of the website so that you don’t have tens of templates.

Overview Steps

There’s going to be a lot of modification required here. So we’ll take it one step at time.

  1. Register some code to function.php. It gets the old shortcode and then builds on top of it.
  2. Make your template using Bricks Template.
  3. Use query instead of the normal loop
  4. Pass the variables to the loop
  5. Go to any post and use the shortcode

Step 1 – Creating the Shortcode

Register this code to function.php. It gets the old shortcode and then builds on top of it.

You can also use any snippet of your choice. Remember, it will require new shortcode.

// Add custom bricks template for Post to render Bricks Section
function custom_bricks_template_shortcode($atts) {
    // Extract the attributes
    $atts = shortcode_atts(
        array(
            'id' => '',
            'state' => '',
        ), 
        $atts, 
        'bricks_template'
    );

    // Pass the attributes to the template
    ob_start();
    set_query_var('bricks_template_state', $atts['state']);
    echo do_shortcode(' . '"]');
    return ob_get_clean();
}
add_shortcode('custom_bricks_template', 'custom_bricks_template_shortcode');

Essentially we’re creating a new shortcode which passes through Bricks original shortcode.

What’s happening here is, we start add new attributes to the shortcode. Store as global query using “set_query_var” the pass it through Bricks Shortcode.

The shortcode will do something like this

[custom_bricks_template id="1706" state="kelantan"]

Id is your template ID and state

Step 2 – Build Your Template

Build the template like you normally do.

image 1 6

Step 3 – Use PHP Query instead of your Normal loop

Make sure to check the query editor to be “On”, then you will have access to ‘Return query parameters’ like this

image 1 7

Step 4 – Pass Variables to Loop

Now you’ve got it open, add the PHP like below

$state = get_query_var('bricks_template_state'); //get this from shortcode
return [
    "objectType" => "post",
    "post_type" => [
        "agent"
    ],
    "orderby" => "rand",
    "tax_query" => [
        [
            "taxonomy" => "state",
            "field" => "slug",
            "terms" => [
                $state
            ]
        ]
    ],
    "post_status" => "publish",
    "paged" => 1,
    "posts_per_page" => "-1"
];

Step 5 – Use the New Shortcode

You can now use the shortcode you’ve newly created

[custom_bricks_template id="1706" state="kelantan"]

Potential Issues

  1. Conflict in the naming. Be sure to use a unique shortcode name
  2. Not rendering loop. You can use echo to see if your code is passed through
<?php
// Make sure the variables are set before trying to echo them
$state = get_query_var('bricks_template_state', 'default_state'); // 'default_state' is optional fallback

echo 'State: ' . esc_html($state) . '<br>';
?>

You Made It to the End!🔥
Free Tips in Your Inbox
Get the latest, evergreen tips to secure, quicken and improve your WordPress in our weekly newsletter.
No spam policy, pure value tips/ tricks
Subscription Form

Written by

Edwin Masripan is the Lead Developer at WebCare with nearly 20 years experience in WordPress web development. He was a speaker at WordCamp (WordPress gathering).
https://webcare.co