Custom Conditions in Bricks Builder

Updated on 22 Jun 2022

This Pro tutorial is a continuation of the previous Conditions in Bricks Builder tutorial.

We are going to keep updating this article with sample code for various useful conditions over time.

List of display conditions covered in this article:

  • Product on backorder?
  • Locale
  • URL has String
  • Don’t output after (or) Expires on
  • Output between beginning date[time] and ending date[time]
  • Output after date[time]

Table of Contents

Product on backorder?

<?php
	if ( wc_get_product() ) {
		$product = wc_get_product();
	} else {
		return false;
	}

	echo $product->is_on_backorder() ? do_shortcode( '[bricks_template id="731"]' ) : do_shortcode( '[bricks_template id="734"]' );
?>

Locale

Can be used with plugins like TranslatePress.

<?php
	echo 'es_ES' === get_locale() ? do_shortcode( '[bricks_template id="731"]' ) : do_shortcode( '[bricks_template id="734"]' );
?>

URL Has String

First define a custom function like so:

<?php 

function bl_url_has_string( $string ) {
    return stripos( $_SERVER['REQUEST_URI'], $string ) !== false;
}

then apply the condition like this:

<?php
	echo bl_url_has_string( 'es' ) ? do_shortcode( '[bricks_template id="731"]' ) : do_shortcode( '[bricks_template id="734"]' );
?>

Don’t output after (or) Expires on

At Settings → General, select your city in the Timezone dropdown.

Then define this custom function:

// Function to check if the given date and time is after the current time.
// Input argument format: Y-m-d H:i:s. Ex.: 2021-03-21 16:29:00
function bl_dont_output_after( $datetime ) {
	// Create a new DateTime object using the user-supplied date string in the timezone set in WP settings.
	$date_input_object = new DateTimeImmutable( $datetime, wp_timezone() );

	// Format the above object into a Unix timestamp.
	$date_input_timestamp = $date_input_object->format( 'U' );
	
	return time() < $date_input_timestamp;
}

and finally apply the condition like this:

<?php echo bl_dont_output_after( '2022-06-22 18:29:00' ) ? do_shortcode( '[bricks_template id="731"]' ) : ''; ?>

Date and time is to be entered in this format: Y-m-d H:i:s

If time is omitted, it will be taken as midnight (12 am).

Output between beginning date[time] and ending date[time]

This can be used to output an element only between the specified dates (and times).

Ex.: Output a section Template between 22nd June 2022 and 26th June 2022.

At Settings → General, select your city in the Timezone dropdown.

Then define this custom function:

// Function to check if the current time is in between the specified dates (and times).
// Input arguments format separated by commas: Y-m-d H:i:s. Ex.: 2021-03-21 16:29:00
function bl_output_between( $datetime1, $datetime2 ) {
	// Create new DateTime objects using the user-supplied date strings in the timezone set in WP settings.
	$date_input_object1 = new DateTimeImmutable( $datetime1, wp_timezone() );
	$date_input_object2 = new DateTimeImmutable( $datetime2, wp_timezone() );

	// Format the above objects into Unix timestamps.
	$date_input_timestamp1 = $date_input_object1->format( 'U' );
	$date_input_timestamp2 = $date_input_object2->format( 'U' );
	
	return ( time() > $date_input_timestamp1 && time() < $date_input_timestamp2 );
}

and apply the condition like this:

<?php echo bl_output_between( '2022-06-22', '2022-06-26' ) ? do_shortcode( '[bricks_template id="731"]' ) : ''; ?>

If time is omitted, it will be taken as midnight (12 am).

Output after date[time]

This can be used to schedule elements to be output at the set date (and optionally, time).

At Settings → General, select your city in the Timezone dropdown.

Then define this custom function:

// Function to check if the current time is after the specified date (and time).
// Input argument format separated by commas: Y-m-d H:i:s. Ex.: 2021-03-21 16:29:00
function bl_output_after( $datetime ) {
	// Create new DateTime object using the user-supplied date string in the timezone set in WP settings.
	$date_input_object = new DateTimeImmutable( $datetime, wp_timezone() );

	// Format the above object into Unix timestamps.
	$date_input_timestamp = $date_input_object->format( 'U' );
	
	return time() > $date_input_timestamp;
}

and apply the condition like this:

<?php echo bl_output_after( '2022-06-25' ) ? do_shortcode( '[bricks_template id="731"]' ) : ''; ?>

If time is omitted, it will be taken as midnight (12 am).

References

https://translatepress.com/docs/restrict-by-language/translation-shortcode/

https://stackoverflow.com/a/62703780/778809