Clear WP Fastest Cache with a PHP Function
In this article
To clear the WP Fastest Cache automatically using a PHP function, you must call the wpfc_clear_cache() function provided by the plugin's internal API. This allows developers to trigger a full cache purge during specific events, such as when a custom post type is updated, when a setting is changed in a third-party plugin, or via a custom admin button, ensuring users always see the most current version of your content without manual intervention.
What You'll Need
- An active installation of the WP Fastest Cache plugin.
- Access to your theme's
functions.phpfile or a custom functionality plugin (like Code Snippets). - SFTP or File Manager access to your server for safety backups.
- Basic knowledge of PHP to ensure the code is placed in the correct hook.
Step-by-Step
- Backup Your Site: Before editing any PHP files, create a full backup of your website or at least a backup of your
functions.phpfile. A single syntax error (like a missing semicolon) can cause a Critical Error (White Screen of Death) on your site. - Identify the Trigger: Decide when the cache should be cleared. Common triggers include the
save_posthook (whenever a post is updated) or a custom action you define. - Verify Plugin Activation: Since
wpfc_clear_cache()is a function defined by the plugin, calling it while the plugin is deactivated will result in a fatal error. You must wrap the function call in afunction_exists()check. - Implement the Code: Add the following code snippet to your
functions.phpfile. In this example, the cache will clear every time a post is published or updated:add_action('save_post', 'clear_wpfc_on_save'); function clear_wpfc_on_save($post_id) { if (function_exists('wpfc_clear_cache')) { wpfc_clear_cache(); } } - Save and Upload: Save the changes to your file and upload it to your server via SFTP or save it within the WordPress Theme Editor.
- Test the Functionality: Go to any post or page, make a small edit, and click Update. Then, check the WP Fastest Cache settings page or use a browser with cache disabled to verify that the cached versions of your pages have been refreshed.
Common Mistakes
- Omitting the function_exists check: This is the most frequent error. If you disable the WP Fastest Cache plugin for maintenance but leave the PHP function in your theme, your entire site will crash because WordPress will try to call a function that no longer exists.
- Infinite Loops: Be careful when using hooks that might trigger another save event. If your cache clearing function triggers a post update, which then triggers the cache clearing function again, you will create an infinite loop that crashes the server.
- Incorrect Hook Usage: Using a hook that fires too frequently (like
initorwp_head) will cause the cache to clear on every single page load, effectively disabling the plugin and slowing down your site significantly.
Tips
- Target Specific Post Types: Instead of clearing the entire cache for every post, use a conditional check within the function to only clear the cache when specific custom post types are updated.
- Use a Child Theme: Never add these functions to a parent theme. When the theme updates, your custom PHP code will be overwritten and deleted. Always use a child theme or a site-specific plugin.
- Log the Execution: If you are unsure if the function is firing, add a temporary
error_log('Cache cleared via PHP');line inside the function to track executions in your server's debug log.
Troubleshooting
If the cache is not clearing as expected, first verify that the WP Fastest Cache plugin is active and that the "Cache System" checkbox is checked in the plugin settings. If the plugin is active but the function isn't working, check your PHP error logs for "undefined function" warnings, which indicates the function_exists check is working but the plugin's internal function name may have changed in a major update.
If your site crashes immediately after adding the code, access your server via FTP, navigate to , and remove the added code. This usually happens due to a copy-paste error or a missing curly bracket } .
Frequently Asked Questions
Does this clear the browser cache or the server cache?
This function clears the server-side cache generated by WP Fastest Cache. It cannot clear the local cache stored in a visitor's web browser.
Will clearing the cache frequently slow down my site?
Yes. The purpose of a cache is to serve static files. If you clear the cache too often, the server must regenerate those files for every visitor, which increases CPU usage and page load times.
Can I clear the cache for only one specific page?
The wpfc_clear_cache() function is designed to purge the entire cache. For granular, page-specific purging, you would typically need to interact directly with the plugin's database options or use a different caching solution with a more robust API.
Is this method compatible with object caching like Redis?
This specific function only targets the file-based cache created by WP Fastest Cache. If you use Redis or Memcached, you will need separate functions (such as wp_cache_flush()) to clear those object caches.
Do I need to be an administrator to trigger this function?
The PHP function runs on the server side. Depending on the hook you use (like save_post), it will run whenever any user with the permission to edit posts saves a change, regardless of their administrative level.