• Resolved generosus

    (@generosus)


    Good Day,

    We are curently using the code snippet provided below to display a message on the front end of our website (i.e., contact page) indicating whether our business is open or not.

    In order for the code snippet to work, we had to disable caching on our contact page.

    Is there any way (via code snippet, ajax call) we can execute the code snippet provided below without the need to disable caching on our contact page?

    Thank you!

    ——————–

    function business_hours_shortcode($atts) {
    if (is_page ('contact')) {
    // Set timezone - change this to match your own.
    date_default_timezone_set('America/Chicago');

    // An array of your opening hours.
    $opening_hours = array(
    'Monday' => array('07:30', '17:30'),
    'Tuesday' => array('07:30', '17:30'),
    'Wednesday' => array('07:30', '17:30'),
    'Thursday' => array('07:30', '17:30'),
    'Friday' => array('07:30', '17:30'),
    'Saturday' => array('Closed'),
    'Sunday' => array('Closed'),
    );

    // An array of your holidays (format: month/day - add zero before single-digit month or day)
    $holidays = array('01/01', '05/26', '07/04', '07/05', '09/02', '11/11', '11/28', '11/29', '12/24', '12/25', '12/31');

    // Get current date, day, and time.
    $current_date = date('m/d');
    $current_day = date('l');
    $current_time = date('H:i');

    // Get today's opening hours.
    $today_hours = $opening_hours[$current_day];

    // Check if today is a holiday.
    if (in_array($current_date,$holidays)) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CLOSED FOR HOLIDAY</span>';
    }

    // Check if we have opening hours for today.
    if (!isset($today_hours) || count($today_hours) < 2) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CLOSED TODAY</span>';
    }

    // Check if current time is between opening hours.
    if ($current_time >= $today_hours[0] && $current_time <= $today_hours[1]) {
    return '<span style="font-weight:600;color:#ffffff;background-color:#00c853;opacity:.7;padding:6px 12px;border-radius:3px;">CURRENTLY OPEN</span>';
    } else {
    return '<span style="font-weight:600;color:#ffffff;background-color:#e14d43;opacity:.7;padding:6px 12px;border-radius:3px;">CURRENTLY CLOSED</span>';
    }
    }
    }
    add_shortcode('business_hours', 'business_hours_shortcode');
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to Prevent PHP Code from Caching’ is closed to new replies.