Title: How to Prevent PHP Code from Caching
Last modified: July 4, 2024

---

# How to Prevent PHP Code from Caching

 *  Resolved [generosus](https://wordpress.org/support/users/generosus/)
 * (@generosus)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-prevent-php-code-from-caching/)
 * 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!
 * ——————–
 *     ```wp-block-code
       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)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-prevent-php-code-from-caching/#post-17870459)
 * Hi [@generosus](https://wordpress.org/support/users/generosus/),
 * What sort of page caching are you using?
 *  Thread Starter [generosus](https://wordpress.org/support/users/generosus/)
 * (@generosus)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-prevent-php-code-from-caching/#post-17871637)
 * Hi [@bungeshea](https://wordpress.org/support/users/bungeshea/),
 * We’re using Speed Optimizer and Cloudflare.
 * Thank you.
 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-prevent-php-code-from-caching/#post-17872449)
 * Unfortunately, I can’t think of a way of bypassing that caching through a snippet.
   Is there a way of excluding the relevant pages from caching? That’s all I can
   think of at this time.
 *  Thread Starter [generosus](https://wordpress.org/support/users/generosus/)
 * (@generosus)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-prevent-php-code-from-caching/#post-17872988)
 * Hi [@bungeshea](https://wordpress.org/support/users/bungeshea/),
 * No worries. We’re already bypassing the caching of the affected page.
 * We found [this great plugin](https://wordpress.org/plugins/content-no-cache/)
   and are waiting for the plugin developer to respond to our inquiry.
 * Cheers!
 *  Thread Starter [generosus](https://wordpress.org/support/users/generosus/)
 * (@generosus)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-prevent-php-code-from-caching/#post-17873098)
 * Hi [@bungeshea](https://wordpress.org/support/users/bungeshea/),
 * Problem solved with another lightweight plugin: [Content No Cache](https://wordpress.org/plugins/content-no-cache/).
 * Check out this [support topic](https://wordpress.org/support/topic/does-not-work-with-shortcodes-4/).
   Perhaps you can work together with the plugin developer to find ways to further
   help your customers and enhance your Pro plugins.
 * Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘How to Prevent PHP Code from Caching’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

## Tags

 * [caching](https://wordpress.org/support/topic-tag/caching/)

 * 5 replies
 * 2 participants
 * Last reply from: [generosus](https://wordpress.org/support/users/generosus/)
 * Last activity: [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-prevent-php-code-from-caching/#post-17873098)
 * Status: resolved