23rd Mar '23
/
0 comments

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 626 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

Bricks Dynamic Data Tag for Text-type Custom Field Value with Word Limit

How to register a new dynamic tag for setting excerpt word limits and outputting an ellipsis (...) at the end.
Categories:
Pro
Conditional Output Based on ACF Date Field in Bricks

Conditional Output Based on ACF Date Field in Bricks

Updated on 1 Aug 2024 Looking to have a Section or other elements in Bricks rendered on the frontend only if today's date is before…
Categories:
Condition for Checking Current Post Category in Bricks

Condition for Checking Current Post Category in Bricks

How we conditionally display an element based on the current post's categories.
Categories:
Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

Conditional Rendering for Logged in/out Users by CSS Classes in Bricks

This tutorial shows how Bricks builder's bricks/element/render filter can be used to conditionally output elements having a class of logged-in to logged-in users only and…
Categories:
Pro
Showing a Popup conditionally in Bricks based on Query Parameter

Showing a Popup conditionally in Bricks based on Query Parameter

Here's two ways a Bricks Popup can be shown on page load depending on URL parameter value.
Categories:
Tags:
Conditions in Bricks Builder

Conditions in Bricks Builder

The current version of Bricks does not have a built-in Conditions feature but that does not mean we can't set them up manually with custom…
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…