Skip to main content

How to Display Custom messages based on Office Hours in WordPress

Have you ever needed to display a custom message to your website visitors based on the time of day? Maybe you want to let them know when your business is open, or encourage them to contact you during certain hours. Recently, one of our clients came to us with a similar request. They run a service-based company with office hours from 8 am to 6 pm and were receiving a lot of phone calls from their website outside of these hours. They wanted to encourage people to use their contact form when the office was closed and came to us for a solution. We wanted the ability to customize the message and not just say “Open” or “Closed” but to actually let the visitor know what time the office would be open next. We came up with a simple and effective code that displays a custom message based on the time of day and day of the week. The shortcode can be used on any page or post in a WordPress website to provide visitors with up-to-date information about the office’s availability.

Improve User Engagement and Conversion Rates with a Time-Based Greeting on your WordPress Website

This code is a WordPress shortcode displaying a message indicating whether a business’s office is open or closed. It determines the current status of the office based on the current day and time in the Pacific Standard Time (PST) timezone. The shortcode can then be added on any page or post in a WordPress website.

Function: is_business_hours()

This function checks whether the current time falls within business hours. It uses the DateTime class to get the current day of the week and the current hour in the PST timezone. The function returns true if the current day is Monday to Friday and the current time is between 8 am and 6 pm PST.

Function: get_office_status_message()

This function determines the message to display based on the current day and time in the PST timezone. It first sets the timezone to PST using date_default_timezone_set() function.

1. Check if the office is currently open

If the current day is Monday to Friday and the current time is between 8 am and 6 pm PST, the function returns a message indicating that the office is currently open and to please give us a call.

2. Check if the office is closed before opening time

If the current day is Monday to Friday and the current time is before 8 am PST, the function returns a message indicating that the office is currently closed. The message includes the time when the office will open and encourages visitors to use the CONTACT US form.

3. Check if the office is closed after closing time

If the current day is Monday to Thursday and the current time is after 6 pm PST, the function returns a message indicating that the office has closed for the day. The message includes the time when the office will reopen the next day and encourages visitors to use the CONTACT US form.

4. Check if the office is closed for the weekend on Friday

If the current day is Friday and the current time is after 6 pm PST, the function returns a message indicating that the office has closed for the weekend. The message includes the time when the office will reopen on Monday and encourages visitors to use the CONTACT US form.

5. Check if the office is closed on Saturday

If the current day is Saturday, the function returns a message indicating that the office is closed for the weekend. The message includes the time when the office will reopen on Monday and encourages visitors to use the CONTACT US form.

6. Check if the office is closed on Sunday

If the current day is Sunday, the function returns a message indicating that the office is closed on weekends. The message includes the time when the office will reopen on Monday and encourages visitors to use the CONTACT US form.

Function: office_status_shortcode()

This function is the actual WordPress shortcode. It simply calls the get_office_status_message() function and returns the message that it generates.

Function: add_shortcode()

This function registers the shortcode with WordPress so that it can be used in pages and posts. It takes two parameters: the name of the shortcode (“office_status”) and the function that generates the message (office_status_shortcode()).

Styling the Message

The code adds a general CSS class of .office-status to the output divider and includes the CSS classes .office-open and .office-closed accordingly.

Finally here is the complete code

You can simply update the messages to suite your needs and drop it into your child theme functions.php file. Once thats done you can use the shortcode [office_status] to display the message anywhere on your website.

// Output message for business hours

function is_business_hours() {
$timezone = 'America/Los_Angeles'; // PST timezone - Change this to your timezone 
$now = new DateTime("now", new DateTimeZone($timezone));
$day_of_week = $now->format('N');
$hour = $now->format('H');
return $day_of_week >= 1 && $day_of_week <= 5 && $hour >= 8 && $hour < 18;
}

function get_office_status_message() {
$timezone = 'America/Los_Angeles';

date_default_timezone_set($timezone);

$now = new DateTime('now');
$today = $now->format('N'); // 1 = Monday, 7 = Sunday
$hour = $now->format('G');

if (is_business_hours()) {
return '<div class="office-status office-open">Our office is currently open! Give us a call!</div>';
} else {
$next_open = new DateTime('now', new DateTimeZone($timezone));

if ($today >= 1 && $today <= 4 && $hour >= 18) { // Monday to Thursday after 6 pm
$next_open->modify('+1 weekday')->setTime(8, 0);
$message = '<div class="office-status office-closed">Our office has closed for the day, but will reopen tomorrow from 8am to 6pm. Please use the CONTACT US form and we will get back to you first thing in the morning. Have a great evening!</div>';
} else if ($today == 5 && $hour >= 18) { // Friday after 6 pm
$next_open->modify('next Monday')->setTime(8, 0);
$message = '<div class="office-status office-closed">Our has closed for the weekend, but will reopen on Monday from 8am to 6pm. Please use the CONTACT US form and we will get back to you first thing Monday morning. Have a great weekend!</div>';
} else if ($today == 6) { // Saturday
$next_open->modify('next Monday')->setTime(8, 0);
$message = '<div class="office-status office-closed">Our office is closed for the weekend, but will reopen on Monday from 8am to 6pm. Please use the CONTACT US form and we will get back to you first thing Monday morning.</div>';
} else { // Sunday or before business hours
$next_open->setTime(8, 0);
$message = '<div class="office-status office-closed">Our office is currently closed, but will be open today from 8am to 6pm. Please use the CONTACT US form and we will get back to you as soon as we get in.</div>';
}

$next_open_formatted = $next_open->format('l, F j, Y \a\t g:i a');
$message .= "<div class='office-next-open'>Our next available time is $next_open_formatted.</div>";
return $message;
}
}

function office_status_shortcode() {
return get_office_status_message();
}

add_shortcode('office_status', 'office_status_shortcode');
Custom Messages, How to Display Custom messages based on Office Hours in WordPress, HS Creative

HS Creative - Austin SEO & Website Design

At HS Creative, we focus on providing tailored digital solutions for small businesses in Austin, Texas. Our services range from custom web design and SEO optimization to social media marketing, pay-per-click ad management, and e-commerce development. Our responsive approach to digital marketing ensures that your website not only looks great but also delivers an excellent user experience that drives more conversions. Whether you need a WordPress website or require help with online advertising, we have the expertise to take your digital presence to the next level.

Contact us today to learn more about our Austin SEO optimization and web design solutions.

Leave a Reply