Conditional content stops displaying after a few hours

This issue occurs on websites that utilize caching and load dynamic content via Ajax.

The issue arises because the dynamic content is loaded via an Ajax request, while the page itself is cached. In WordPress, a security token called a nonce is used to verify that the request is legitimate. However, when the page is cached, the nonce generated for the initial page load may not match the nonce expected by the Ajax request. This mismatch prevents the proper loading of the dynamic content, leading to the issue.

The Solution

The key is to get the Cache Lifespan value set just low enough that the cache will expire before the nonce does. We strongly recommend setting the cache lifespan to 6 hours or less.

How to set the cache lifespan in some of the most popular caching plugins:

  1. WP Super Cache
  2. W3 Total Cache
  3. WP Rocket
  4. WP Fastest Cache
  5. WPOptimize

WPOptimize users

If you are using WPOptimize, there’s another configuration that might cause a similar issue. Follow the instructions below if setting the cache lifespan to 6 hours didn’t work:

  1. On your WordPress dashboard, go to wp optimize->minify->javascript
  2. Add the following string (exactly as it is) to the Exclude JavaScript from processing field: