Bricks Element Condition: During Opening Hours
Adds a "During opening hours" condition to Bricks, so a "Call us now" button or live chat block only shows while your business is open.
Opens WPIDE on
Code
// Opening hours per weekday (1 = Monday ... 7 = Sunday), 24-hour "HH:MM".
$wpide_bricks_hours = array(
1 => array( '09:00', '17:30' ),
2 => array( '09:00', '17:30' ),
3 => array( '09:00', '17:30' ),
4 => array( '09:00', '17:30' ),
5 => array( '09:00', '16:00' ),
);
add_filter( 'bricks/conditions/groups', function ( $groups ) {
foreach ( $groups as $group ) {
if ( isset( $group['name'] ) && 'wpide' === $group['name'] ) {
return $groups;
}
}
$groups[] = array( 'name' => 'wpide', 'label' => 'Site rules' );
return $groups;
} );
add_filter( 'bricks/conditions/options', function ( $options ) {
$options[] = array(
'key' => 'wpide_open_now',
'label' => 'During opening hours',
'group' => 'wpide',
'compare' => array(
'type' => 'select',
'options' => array( '==' => 'is', '!=' => 'is not' ),
'placeholder' => 'is',
),
'value' => array( 'type' => 'select', 'options' => array( 'open' => 'open' ), 'placeholder' => 'open' ),
);
return $options;
} );
add_filter( 'bricks/conditions/result', function ( $result, $condition_key, $condition ) use ( $wpide_bricks_hours ) {
if ( 'wpide_open_now' !== $condition_key ) {
return $result;
}
$now = new DateTime( 'now', wp_timezone() );
$day = (int) $now->format( 'N' );
$time = $now->format( 'H:i' );
$open = isset( $wpide_bricks_hours[ $day ] ) && $time >= $wpide_bricks_hours[ $day ][0] && $time < $wpide_bricks_hours[ $day ][1];
return ( isset( $condition['compare'] ) && '!=' === $condition['compare'] ) ? ! $open : $open;
}, 10, 3 );
Notes
Hours use the site timezone from Settings > General. Pages using it should not be cached for long.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.