User Agents

Web browsers and everyday web tech, explained

Clear WP Fastest Cache via PHP Function Calls

· 4 min read · Cookies and Cache

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

You can clear the WP Fastest Cache plugin programmatically by calling one of its built-in PHP functions, WpFastestCache::deleteCache() or wpfc_clear_all_cache(). This is useful when you need to purge the cache automatically after custom code runs, such as a plugin update, a form submission, or a scheduled event. No extra plugin is required, and the process takes only a few lines of code.

What You'll Need

  • A WordPress site with the WP Fastest Cache plugin installed and active.
  • Access to your theme's functions.php file, a custom plugin file, or a code snippet plugin.
  • Basic familiarity with PHP and WordPress hooks.

Step-by-Step

  1. Identify the correct function. WP Fastest Cache provides two primary methods: WpFastestCache::deleteCache() (a class method) and wpfc_clear_all_cache() (a global function). The latter is simpler and works in most versions.
  2. Choose where to add the code. You can add it to your theme's functions.php, a custom plugin, or a code snippet plugin like Code Snippets. A custom plugin is best if you want the code to survive theme changes.
  3. Write a simple function that calls the cache clear. For example:
    function my_clear_wpfc_cache() {
        if (function_exists('wpfc_clear_all_cache')) {
            wpfc_clear_all_cache();
        }
    }
  4. Hook the function to the desired event. If you want to clear the cache after a post is saved, use the save_post hook:
    add_action('save_post', 'my_clear_wpfc_cache');
    For a scheduled event, use a custom cron hook. For a form submission, hook into the form plugin's action.
  5. Test the function. Save a post or trigger the event, then check the WP Fastest Cache settings page to see if the cache size has reset.
  6. Consider using the class method for more control. If you need to clear only certain cache types, you can call the class method directly:
    if (class_exists('WpFastestCache')) {
        $wpfc = new WpFastestCache();
        $wpfc->deleteCache();
    }
  7. Add a fallback for safety. Always check that the function or class exists before calling it, to avoid fatal errors if the plugin is deactivated.

Common Mistakes

  • Calling the function before WordPress loads. The cache functions are only available after the plugin is loaded. If you call them too early, you'll get a fatal error. Always use a hook like init or later.
  • Not checking if the function exists. If the plugin is deactivated or updated, the function name may change. Use function_exists() or class_exists() to avoid errors.
  • Clearing the cache on every page load. This defeats the purpose of caching and can slow down your site significantly. Only clear when content changes.
  • Using the wrong hook. For example, using init to clear cache on every request is a common mistake. Use specific hooks like save_post or wp_update_nav_menu.

Tips

  • Use a custom plugin instead of functions.php. This keeps your code separate from the theme, so it remains active even if you switch themes.
  • Log the cache clear. Add error_log('Cache cleared by custom code'); to help debug if something goes wrong.
  • Combine with other cache plugins. If you use multiple caching layers (like object cache), you may need to clear those too. WP Fastest Cache only clears its own cache.
  • Test on a staging site first. Always test the code in a staging environment before deploying to production.

Troubleshooting

If the cache doesn't clear, try these steps:

  • Check that the function exists. Add a quick var_dump(function_exists('wpfc_clear_all_cache')); in your code and load the page. If it returns false, the plugin isn't loaded or the function name is different.
  • Verify the hook is firing. Add a log statement inside your function to confirm it runs. If it doesn't, the hook might be wrong or the event isn't triggered.
  • Look for PHP errors. Enable WP_DEBUG in wp-config.php to see if there are any fatal errors or warnings.
  • Check if the cache is being regenerated. If you clear the cache but a page is visited immediately after, the cache may be recreated before you check. Wait a few seconds or clear again.
  • Update the plugin. Older versions may have different function names. Check the plugin's documentation or source code for the current function.

Frequently Asked Questions

Can I clear only the minified CSS/JS cache?

WP Fastest Cache's deleteCache() method clears all cache types, including minified files. There is no built-in function to clear only a subset. You may need to manually delete files in the cache directory if you need selective clearing.

Will this work with WP-CLI?

Yes, you can call the same PHP functions in a WP-CLI command by using wp eval or a custom WP-CLI command. For example: wp eval 'if (function_exists("wpfc_clear_all_cache")) wpfc_clear_all_cache();'

Is it safe to call this on every save?

It is safe but not efficient. Clearing the entire cache on every post save can be resource-intensive if you have a large site. Better to use specific hooks like save_post only when needed, or use the plugin's built-in options to clear on update.

What if I use a page builder like Elementor?

Page builders often have their own cache clearing. You can combine both by calling WP Fastest Cache's function after the page builder's save action. Just add your code to the appropriate hook.

Does this clear the cache for all users?

Yes, wpfc_clear_all_cache() clears the entire cache for all visitors. There is no per-user cache in WP Fastest Cache.

Can I use this in a mu-plugin?

Yes, you can place the code in a must-use plugin (wp-content/mu-plugins/). It will load automatically and be available early, but ensure you still hook actions properly.