How to Fix the admin-ajax.php Caching Issue in LiteSpeed Cache
Content
A common issue that arises when using the LiteSpeed Cache plugin alongside dynamic plugins, such as WPML or e-commerce currency switchers, is the caching of the admin-ajax.php file. This can break functionality that relies on real-time, non-cached AJAX requests. This guide will explain why this happens and how to resolve it.
Why Does This Happen?
The admin-ajax.php file is a core WordPress file used to handle AJAX requests. These requests are dynamic by nature and often involve user-specific data, such as shopping cart contents or currency selections. When a caching plugin like LiteSpeed Cache serves a cached version of this file, it returns the same response to every user, breaking the intended dynamic behavior. As seen in user reports, support teams for other plugins will often identify this as the root cause when their functionality fails only when LiteSpeed Cache is active.
How to Exclude admin-ajax.php from Caching
The solution is to instruct LiteSpeed Cache not to cache the admin-ajax.php file. This is done by adding an exclusion rule within the plugin's settings.
- Navigate to the Cache Settings: In your WordPress admin dashboard, go to LiteSpeed Cache > Cache > Advanced.
- Find the Exclusion Settings: Look for the setting labeled "Do Not Cache URIs".
- Add the Exclusion: In the input field, add the following line:
/wp-admin/admin-ajax.php - Save Changes: Scroll down and click "Save Changes" to apply the new rule.
After saving, it's crucial to purge all existing cache to ensure the new rule takes effect immediately. You can do this from the LiteSpeed Cache > Toolbox > Purge menu by selecting "Purge All".
Testing the Fix
Once you have added the exclusion and purged the cache, test the functionality that was previously broken. For example, if you were troubleshooting a WPML currency switcher, try switching currencies on a product page. The action should now work correctly without displaying outdated or incorrect information.
Additional Considerations
If the problem persists after this change, consider the following:
- Query Strings: Some AJAX requests use unique query strings. You may need to investigate if additional exclusions are needed in the "Do Not Cache Query Strings" setting.
- Server-Level Caching: If you are on a LiteSpeed server, there might be server-level page caching rules at play. In such cases, you may need to consult your hosting provider to ensure the exclusion is applied at the server level as well.
By excluding admin-ajax.php from the cache, you allow dynamic, user-specific requests to function as intended while still benefiting from LiteSpeed Cache's performance optimizations on the rest of your site.
Related Support Threads Support
-
admin-ajax.php cachehttps://wordpress.org/support/topic/admin-ajax-php-cache-2/
-
Drop Query String Option not saving in Network installationhttps://wordpress.org/support/topic/drop-query-string-option-not-saving-in-network-installation/
-
Usual error: QUIC.cloud service nodes cannot reach your WP RESThttps://wordpress.org/support/topic/usual-error-quic-cloud-service-nodes-cannot-reach-your-wp-rest/
-
Known issue Litespeed Cache vs Patchstack blocking connectivityhttps://wordpress.org/support/topic/known-issue-litespeed-cache-vs-patchstack-blocking-connectivity/
-
Old Folders & Fileshttps://wordpress.org/support/topic/old-folders-files/
-
Image Optimization to AVIF not Converting WEBP Imageshttps://wordpress.org/support/topic/image-optimization-to-avif-not-converting-webp-images/
-
Custom vary and QC CDNhttps://wordpress.org/support/topic/custom-vary-and-qc-cdn/
-
Remove Unused CSShttps://wordpress.org/support/topic/remove-unused-css-26/
-
How to disable image optimizations for a specific .php file?https://wordpress.org/support/topic/how-to-disable-image-optimizations-for-a-specific-php-file/
-
Cron reschedule event error for hook: litespeed_task_vpihttps://wordpress.org/support/topic/cron-reschedule-event-error-for-hook-litespeed_task_vpi/
-
jpeg fallbacks when browser doesn’t support webphttps://wordpress.org/support/topic/jpeg-fallbacks-when-browser-doesnt-support-webp/
-
Server error log Undefined array key “guest_uas”https://wordpress.org/support/topic/server-error-log-undefined-array-key-guest_uas/
-
Litespeed Avatar DB table filling with millions of rowshttps://wordpress.org/support/topic/litespeed-avatar-db-table-filling-with-millions-of-rows/
-
Comment count incorrect when enabled Object Cachehttps://wordpress.org/support/topic/comment-count-incorrect-when-enabled-object-cache/
-
Avif files not served for background imageshttps://wordpress.org/support/topic/avif-files-not-served-for-background-images/