Query String Value in Bricks

This Pro tutorial provides the steps to output the value of a specified query string, also known as URL parameter in Bricks.

We shall also show how elements can be conditionally rendered only if the query string in the URL of a given page has

  1. a value
  2. a specified value

The example considered in this tutorial is subid URL parameter.

Ex.:

example.com/?subid=newsletter

You should replace all instances of subid with your query variable as applicable.

Step 1

Let’s inform WordPress that there’s a query variable called subid via this filter:

// Register `subid` custom query variable.
add_filter( 'query_vars', function( $qvars ) {
	$qvars[] = 'subid';

	return $qvars;
});

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

Step 2

Whitelist the get_query_var function. This function takes a query variable as its argument and returns its value.

Ex.:

<?php 

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

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

More info on whitelisting can be found here.

Step 3

In Bricks editor to output your query variable value, use this dynamic data tag:

{echo:get_query_var(subid)}

Step 4

If you want to output an element only if your query variable is not empty:

Step 5

If you want to output an element only if your query variable has a specific value: