How to Control WP-Optimize Cache Purging When Publishing or Editing Content
Content
One of the most common points of discussion for users of the 'WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance' plugin is its automatic cache purging behavior. Users often want the cache to update automatically when they publish new content, but the plugin's default settings can sometimes be too aggressive or not aggressive enough, leading to outdated content or unnecessary full cache clears.
Understanding the Default Behavior
By default, the plugin is designed to automatically purge the cache for a specific post or page when it is published or updated. It should also clear associated content like category and tag archives. However, based on user reports, this behavior can be inconsistent. Some users find the cache is not cleared at all for new content, while others report that editing a simple draft triggers a full site cache purge, which can be counterproductive for site performance.
Common Solutions and Code Snippets
Since the plugin's interface does not offer granular controls for these specific purging behaviors, the most common solutions involve adding custom code to your theme's functions.php file or using a code snippets plugin.
1. Force Purge on All Post/Page Updates
If your new or edited content is not appearing until you manually clear the cache, you can force a full purge on any update. This is a broad solution but ensures content is always fresh.
add_filter( 'wpo_purge_all_cache_on_update', '__return_true' );
2. Purge Cache Only When a Post is Published
To avoid purging the cache every time a post is edited or saved as a draft, you can use a more targeted filter that only triggers a purge when a post's status is changed to 'publish'. This is useful for news sites that publish content frequently.
add_filter('wpo_purge_all_cache_on_update', function($purge, $post_id) {
if (get_post_status($post_id) === 'publish') {
$purge = true;
}
return $purge;
}, 10, 2);
3. Set a Custom Cache Lifespan
For sites that update data very frequently (e.g., every 15-20 minutes), the hourly cache lifespan options in the UI may not be sufficient. You can use a filter to set the lifespan in seconds. Remember to reactivate the plugin or clear the cron event for this to take effect.
add_filter('wpo_option_key_page_cache_length', 'my_prefix_page_cache_length');
function my_prefix_page_cache_length() {
return 60 * 15; // 15 minutes in seconds
}
Important Considerations
- Testing: Always test code snippets on a staging site before implementing them on your live website.
- Child Theme: If adding code to your
functions.phpfile, use a child theme to prevent your changes from being overwritten during theme updates. - Plugin Conflicts: Some issues, like the entire cache purging when a new post is created, have been acknowledged by the 'WP-Optimize' team as bugs in past versions. Ensure your plugin is updated to the latest version, as some reported behaviors may have been resolved.
- CDN and Other Caches: If you are using a Content Delivery Network (CDN) or another caching plugin in conjunction with WP-Optimize, you may need to configure cache purging there as well.
While the 'WP-Optimize' plugin provides powerful caching and optimization tools, fine-tuning its automatic purging behavior often requires these custom code solutions. The community has found these filters to be effective for managing cache based on their specific content update workflows.
Related Support Threads Support
-
Post revisionshttps://wordpress.org/support/topic/post-revisions-4/
-
Page cache is totally cleared when new posts are createdhttps://wordpress.org/support/topic/page-cache-is-totally-cleared-when-new-posts-are-created/
-
Clear cache with post schedulinghttps://wordpress.org/support/topic/clear-cache-with-post-scheduling/
-
Publishing a post or CPT clears the cache of all posts of the same post type.https://wordpress.org/support/topic/publishing-a-post-or-cpt-clears-the-cache-of-all-posts-of-the-same-post-type/
-
clean_post_cache hookhttps://wordpress.org/support/topic/clean_post_cache-hook/
-
How to clean cache automatically when a page, article is created or edited?https://wordpress.org/support/topic/how-to-clean-cache-automatically-when-a-page-article-is-created-or-edited/
-
Exclude cache Instagram Feedhttps://wordpress.org/support/topic/exclude-cache-instagram-feed/
-
How to clean cache when a content is edited?https://wordpress.org/support/topic/how-to-clean-cache-when-a-content-is-edited/
-
Wp-optimize clearing all cache after every new posthttps://wordpress.org/support/topic/wp-optimize-clearing-all-cache-after-every-new-post/
-
Feature Request: LifeSpanhttps://wordpress.org/support/topic/feature-request-lifespan/
-
Stop auto flushing cachehttps://wordpress.org/support/topic/stop-auto-flushing-cache/
-
Cache Not Rebuilt Immediately Upon Being Cleared.https://wordpress.org/support/topic/cache-not-rebuilt-immediately-upon-being-cleared/
-
Is there a way to call partial clear cache by hooks?https://wordpress.org/support/topic/is-there-a-way-to-call-partial-clear-cache-by-hooks/
-
site breaks after updateshttps://wordpress.org/support/topic/site-breaks-after-updates-2/
-
Keeping revisions during optimization does not workhttps://wordpress.org/support/topic/keeping-revisions-during-optimization-does-not-work/
-
Automatically update cash when you post a new posthttps://wordpress.org/support/topic/automatically-update-cash-when-you-post-a-new-post/
-
Auto rebuild cache after wp update?https://wordpress.org/support/topic/auto-rebuild-cache-after-wp-update/
-
Updating a post draft automatically triggers WP-Optimize to clear the cachehttps://wordpress.org/support/topic/updating-a-post-draft-automatically-triggers-wp-optimize-to-clear-the-cache/
-
Prevent Caching On First Openhttps://wordpress.org/support/topic/prevent-caching-on-first-open-2/
-
Database “keep data” setting not functioninghttps://wordpress.org/support/topic/database-keep-data-setting-not-functioning/
-
cleaning database tables for a specific data range?https://wordpress.org/support/topic/cleaning-database-tables-for-a-specific-data-range-2/
-
Code Snippet to clear Individual Page Cache on Commenthttps://wordpress.org/support/topic/code-snippet-to-clear-individual-page-cache-on-comment/
-
cache lifespan not workinghttps://wordpress.org/support/topic/cache-lifespan-not-working/
-
Scheduled Posts Not Showinghttps://wordpress.org/support/topic/scheduled-posts-not-showing-2/
-
How to set cache life span to 15minhttps://wordpress.org/support/topic/how-to-set-cache-life-span-to-15min/
-
Automatically purge all cache when new post is publishedhttps://wordpress.org/support/topic/automatically-purge-all-cache-when-new-post-is-published/
-
Page Cache – Elementorhttps://wordpress.org/support/topic/page-cache-elementor/
-
Purge Cache on ACF Save Posthttps://wordpress.org/support/topic/purge-cache-on-acf-save-post/
-
Cache managmenthttps://wordpress.org/support/topic/cache-managment/
-
Set Revision number of Pages?https://wordpress.org/support/topic/set-revision-number-of-pages/
-
Can I clear cache for specific post onlyhttps://wordpress.org/support/topic/can-i-clear-cache-for-specific-post-only/
-
Clean all post revisionshttps://wordpress.org/support/topic/clean-all-post-revisions/