How to Exclude Pages and URLs from Caching in WP-Optimize
Content
Configuring caching exclusions is a common task for users of the 'WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance' plugin. This guide explains how the exclusion settings work and provides solutions for the most frequently encountered issues.
Understanding Cache Exclusion Settings
The plugin provides several methods to control what content is cached. The primary settings are found under Cache > Advanced.
1. URLs to Exclude from Caching
This is the most straightforward method for excluding entire pages or sections of your site. You can list specific URLs or paths, using a wildcard (*) to match multiple items.
- How it works: Enter one path per line. The system matches the path of the current URL against your list.
- Examples:
- To exclude a single page:
/my-specific-page/ - To exclude all pages beginning with a path:
/forum/*(This will exclude/forum/,/forum/topic/, etc.) - To exclude a specific file in the root:
/connection.php
- To exclude a single page:
- Important Limitation: As seen in the sample threads, the wildcard functionality is basic. It does not support full regular expressions. For instance, excluding a pattern like
/shop/page/[0-9]{2,}(to skip pages 10 and above) is not currently possible with the standard interface.
2. Cookies Which Prevent Caching
This setting is designed to dynamically serve an uncached version of a page when a specific cookie is detected in the user's browser.
- How it works: If a cookie listed in this field is present, the page will not be served from the cache. This is crucial for pages that should be unique to a user, like those containing a shopping cart.
- Example: To prevent caching for visitors who have items in their cart, you would add cookies like
woocommerce_items_in_cartandwoocommerce_cart_hash, one per line. When a user adds an item to their cart, this cookie is set, and the cached page is bypassed.
3. Query Strings (URL Parameters)
By default, URLs with query strings (e.g., ?utm_source=newsletter) are often not cached. However, you may want to ignore common marketing parameters so that the core page is still cached.
- How it works: This requires using a specific filter hook,
wpo_cache_ignore_query_variables, in your theme'sfunctions.phpfile or a custom plugin. This filter allows you to define an array of query variables that should be stripped from the URL before checking the cache. - Example Code:
add_filter('wpo_cache_ignore_query_variables', 'my_ignore_query_variables'); function my_ignore_query_variables($exclude) { $new_variables = array( 'utm_source', 'utm_medium', 'utm_campaign', 'gclid' ); return array_merge($exclude, $new_variables); }
Common Issues and Solutions
Problem: My exclusion rule for a path with a wildcard (e.g., /tests/*) is not working as expected. The main path is cached, but sub-paths are not.
Solution: This indicates a potential issue with how the path matching function interprets the rule. Test with different patterns. Sometimes being more specific (e.g., /tests/* and /tests) can help. This is a known area where the plugin's matching logic can be improved.
Problem: I need to exclude a specific physical file in my site's root directory (e.g., yandexinfo.php).
Solution: Simply adding the filename (e.g., /yandexinfo.php) to the "URLs to exclude from caching" list is the correct method, as confirmed in the sample threads.
Problem: I excluded a page from caching, but its CSS/JS files are still minified and merged.
Solution: This is normal behavior. The cache exclusion settings only affect page caching. The minify and merge settings are separate and apply site-wide. A page excluded from cache will still load the minified assets if that feature is enabled.
Problem: My custom filter for wpo_cache_ignore_query_variables is not working.
Solution: Ensure the code is correctly placed in your active theme's functions.php file. If the problem persists, there may be a conflict with another plugin or theme. Testing with all other plugins disabled can help identify a conflict.
What You Cannot Do (Yet)
- Regex Exclusions: The plugin does not currently support full regular expressions for cache exclusions. This is a popular feature request that the development team is aware of.
- Role-Based Cache Purging: The ability to purge the entire cache is intentionally restricted to administrators for security reasons. Editors can purge the cache of individual posts they update, but not the entire site cache.
Related Support Threads Support
-
Excluding woocommerce cookies from cachehttps://wordpress.org/support/topic/excluding-woocommerce-cookies-from-cache/
-
Ignoring query strings with premium versionhttps://wordpress.org/support/topic/ignoring-query-strings-with-premium-version/
-
Exclude pages from cache and effect on minify merged fileshttps://wordpress.org/support/topic/exclude-pages-from-cache-and-effect-on-minify-merged-files/
-
“URLs to exclude from caching” doesn’t work how it can be expectedhttps://wordpress.org/support/topic/urls-to-exclude-from-caching-doesnt-work-how-it-can-be-expected/
-
how to use the cache exlusion?https://wordpress.org/support/topic/how-to-use-the-cache-exlusion/
-
Conditional tags to exclude from cachinghttps://wordpress.org/support/topic/conditional-tags-to-exclude-from-caching/
-
Ignoring some query variableshttps://wordpress.org/support/topic/ignoring-some-query-variables/
-
Nginxhttps://wordpress.org/support/topic/nginx-29/
-
Feature request: exclusion rules with regular expressionshttps://wordpress.org/support/topic/feature-request-exclusion-rules-with-regular-expressions/
-
Exclude from cachinghttps://wordpress.org/support/topic/exclude-from-caching-2/