Clear WP Fastest Cache Programmatically
In this article
Clearing WP Fastest Cache programmatically allows developers to automate cache purging through PHP functions, ensuring content updates appear immediately without manual intervention. This is especially useful for dynamic sites that update content via custom scripts, APIs, or form submissions. The WP Fastest Cache plugin provides a built-in function to clear the entire cache or specific parts of it directly from PHP code, enabling full integration into themes, plugins, or custom logic.
What You'll Need
- WordPress site with the WP Fastest Cache plugin installed and activated
- Access to your theme’s functions.php file or a custom plugin file
- Basic knowledge of PHP and WordPress hooks
- Administrator privileges on the WordPress site
Step-by-Step
- Verify the plugin is active: Before using any functions, ensure WP Fastest Cache is installed and active. You can check this under Plugins > Installed Plugins in the WordPress admin dashboard. The function will not work if the plugin is inactive or uninstalled.
- Locate the target PHP file: Identify where you want to trigger the cache clear. This could be in your theme’s
functions.php, a custom plugin file, or a script that runs after content updates. Avoid editing core WordPress files or plugin files directly, as updates will overwrite your changes. - Check for the existence of the cache clearing function: Since the WP Fastest Cache function may not be available during early WordPress loading phases, wrap your call in a conditional check. Use
function_exists('wpfc_clear_all_cache')to avoid fatal errors if the plugin is disabled or not loaded. - Call the cache clearing function: Use the built-in function
wpfc_clear_all_cache()to purge the entire cache. Insert this function call where needed—for example, after updating a post via code or after a form submission. A basic implementation looks like:if (function_exists('wpfc_clear_all_cache')) { wpfc_clear_all_cache(); }This safely clears all cached files generated by WP Fastest Cache.
- Use WordPress hooks appropriately: To automate cache clearing, hook the function into relevant WordPress actions. For example, to clear cache after a post is updated, use the
save_posthook:function my_clear_wpfc_after_save($post_id) { if (function_exists('wpfc_clear_all_cache')) { wpfc_clear_all_cache(); } } add_action('save_post', 'my_clear_wpfc_after_save');This ensures the cache is cleared every time a post or page is saved.
- Clear cache selectively (optional): While
wpfc_clear_all_cache()clears everything, you may want to clear only the cache for a specific page or post. WP Fastest Cache does not expose a public function for single-page clearing by default, but you can simulate this by deleting individual cache files via PHP if you know the file path structure. However, this is not recommended unless you’re certain of the cache directory layout and file naming conventions, as it risks incomplete or incorrect purging. - Test the implementation: After adding the code, make a change that should trigger the cache clear (e.g., edit and save a post). Then visit the front end and verify that updated content appears immediately. You can also check the WP Fastest Cache dashboard or cache directory to confirm files have been removed.
- Secure your implementation: Ensure cache-clearing code only runs when necessary and not on every page load. Avoid placing
wpfc_clear_all_cache()in template files likeheader.phporfooter.php, as this would clear the cache on every visit, defeating the purpose of caching.
Common Mistakes
One frequent error is calling wpfc_clear_all_cache() without checking if the function exists, leading to fatal PHP errors when the plugin is inactive. Another is placing the function in the wrong hook—such as init or wp_head—which can cause performance issues or unintended cache purges. Developers sometimes forget that clearing the entire cache affects all pages, not just the updated one, so use it judiciously. Lastly, editing theme files without using a child theme risks losing changes during updates.
Tips
Use WordPress transients or custom flags to limit how often cache is cleared—for example, only clear once per content update batch. Consider combining this with browser cache headers or CDN purge commands if you’re using external services. Always test on a staging site first. If you’re building a plugin, encapsulate the cache-clearing logic in a separate function for reusability. Logging cache-clear events (via error_log or a debug plugin) can help track when and why purges occur.
Troubleshooting
If the cache isn’t clearing, first verify the plugin is active and the function name is correct—some versions may use slight variations, though wpfc_clear_all_cache is standard. Check PHP error logs for fatal errors caused by calling the function prematurely. If using a hook, ensure it fires at the right time—save_post runs multiple times (for revisions, auto-drafts), so add conditions to run only on publish or update. If cache files remain, manually inspect the /wp-content/cache/wpfc/ directory to confirm files exist and are being deleted. If using object caching or a multi-server setup, ensure all servers sync cache changes or clear them individually.
Frequently Asked Questions
Does wpfc_clear_all_cache() clear mobile and desktop caches?
Yes, it clears all cached versions, including mobile and desktop, as well as any gzip variants if enabled.
Can I clear cache for a single page only?
Not directly via public functions. WP Fastest Cache doesn’t provide a built-in function for single-page clearing. You’d need to manually delete the specific cache file, which is error-prone and not recommended.
Is it safe to use wpfc_clear_all_cache() in a live environment?
Yes, as long as it’s used conditionally and not on every page load. Clearing the cache temporarily slows down the next visitor until new cache is generated.
What happens if WP Fastest Cache is deactivated after I add the code?
If you wrap the call in function_exists(), nothing happens—no error. Without the check, your site will crash with a fatal error.
Does this function clear CDN or browser cache?
No, it only clears server-side cache files. CDN or browser caches require separate purging methods.
Can I trigger this from a REST API endpoint?
Yes. As long as the plugin is loaded and you’re in a WordPress context, you can call wpfc_clear_all_cache() inside a custom REST callback.