How to Use

Download > Install > Activate. To edit, use your favorite IDE or just edit within the WordPress plugin editor. Change the open and close times to suite your needs. Times are based on server time so adjust accordingly.

Example

In the code below I am open 9-5 mountain or 16:00 – 11:59 UTC (server time). The last line of the code allows for the short code to be used within a WordPress menu to easily add to the header.

Source

This code can be pasted into the functions.php file of your child theme or, download and install the plugin for simple code activation.

function build_viawebs_business_hours_indicator_shortcode(){
$now = new Datetime("now");
$opentime = new DateTime('15:00');
$closetime = new DateTime('23:00');

if($now >= $opentime && $now <= $closetime){
    // between times
    $output = '<div style="padding: 5px 10px; background: #0cb90c; color: #ffffff; max-width: 100px; text-align: center; font-weight: 500; border-radius: 15px;">Open</div>';
} else {
    // not between times
    $output = '<div style="padding: 5px 10px; background: #ff0000; color: #ffffff; max-width: 100px; text-align: center; font-weight: 500; border-radius: 15px;">Closed</div>';
}
 	// $output = '<div>' . $now->format('Y-m-d H:i') . '</div>'; //uncomment line to see current server time
	return $output;
}
add_shortcode( 'vwopenclose', 'build_viawebs_business_hours_indicator_shortcode');

add_filter('wp_nav_menu_items', 'do_shortcode');