Weekday Condition in Bricks

As of Bricks 1.7, the built-in “Weekday” condition does not work correctly.

Wrong output where it is Thursday on the given day when this was tested

This is most likely due to it not taking the site’s timezone (set in WordPress settings) into account.

To work around this, let’s define a custom function and use it with the Dynamic data condition.

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

// Function to check if today is the specified (three letter format) weekday.
// Sample usage: bl_is_weekday( 'Thu' )
function bl_is_weekday( $weekday ) {
	$today = wp_date( 'D' );

	return $today === $weekday;
}

and whitelist it.

Ex.:

<?php 

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

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

More info on whitelisting can be found here.

and apply it like this:

{echo:bl_is_weekday(Thu)}

Replace Thu with the three-letter week you want to check against.

To output the element when it is NOT the specified weekday, check against 0 instead of 1.

Alternatively, you could simply do:

{echo:wp_date(D)}

to render the element on the specified weekday.

This way, you don’t have to register a custom function.

Get access to all 524 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Conditionally Outputting Based on Query Count in Bricks

Conditionally Outputting Based on Query Count in Bricks

Update on 22 Sep 2023: There's a much simpler built-in method now. See this post. In the Bricks Facebook group a user asks: Is there…
Categories:
How to apply a custom query var to multiple query loops in Bricks

How to apply a custom query var to multiple query loops in Bricks

In this tutorial, we'll learn how to apply a PHP filter to modify the query var of multiple query loops in a single function Introduction…
Categories:
Pro
Conditional Rendering Based On Meta Box Group’s Sub Field

Conditional Rendering Based On Meta Box Group’s Sub Field

This Pro tutorial shows how we can output an element on a single post pages only if a specified sub-field (custom field created using Meta…
Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Pro
Conditionally outputting an element in a Query Loop based on date-type custom field

Conditionally outputting an element in a Query Loop based on date-type custom field

This Pro tutorial shows how to output an element in a Bricks' query loop only if the value of a date-type of custom field for…
Categories:
Is WooCommerce Cart Empty Condition in Bricks

Is WooCommerce Cart Empty Condition in Bricks

Looking to conditionally render an element depending on whether the user's cart is empty or not when using WooCommerce? and use it with a Dynamic…
Pro
Custom Dynamic Data Tags in Bricks

Custom Dynamic Data Tags in Bricks

As of Bricks 1.7, the built-in "Weekday" condition does not work correctly. This is most likely due to it not taking the site's timezone (set…
Render an element having a specific HTML ID based on value of a custom field

Render an element having a specific HTML ID based on value of a custom field

This tutorial provides the steps to use Bricks' bricks/element/render filter to conditionally output an element that has the specified HTML ID based on the value…
Categories:
Pro
Conditional Rendering in Bricks Based on CSS Class

Conditional Rendering in Bricks Based on CSS Class

Updated on 19 Jun 2024 A user asks: Is there a way to apply the same conditions to several elements like we set styles appending…
Categories: