Clear Cache Everywhere
The Clear Cache Everywhere plugin has a helper function and some useful hooks for developers to use to further enhance its functionality.
cceverywhere_clear_all( $log_results = false );
$log_results (bool): When true, writes the saved result of each action to the PHP debug log. Default false.
Things to know:
- The function does nothing unless the current user is logged in and has the
manage_optionscapability. It will not run from cron, WP-CLI, or front-end visitors. - Actions with the
pagerun context run immediately in the same request, with no page reload. - A failure in one action does not stop the others.
- Each action’s result is saved and appears in the Logs tab, with the context labeled “Helper Function”.
Example Usage
/**
* Clear everything after an administrator updates a plugin or theme from the dashboard.
* Automatic background updates run without a logged-in user, so this will not run for those.
*/
add_action( 'upgrader_process_complete', function() {
if ( function_exists( 'cceverywhere_clear_all' ) ) {
cceverywhere_clear_all();
}
} );cceverywhere_admin_bar_enabled
TYPE ⇢ Filter
This hook allows developers to disable the plugin’s admin bar button and related scripts if you only want to use it from the Settings page.
Example Usage
/**
* Disable the Clear Cache admin bar button completely.
*/
add_filter( 'cceverywhere_admin_bar_enabled', '__return_false' );cceverywhere_custom_settings
TYPE ⇢ Filter
This filter was updated in version 1.2.0.
It allows developers to add custom fields to the plugin’s settings page, including defining custom clearing actions.
Each field can include a callback property to override the default clearing behavior, and a run_context property ('ajax' or 'page') to control when the action is executed.
This is now the preferred way to add or customize clearing actions, replacing the deprecated cceverywhere_before_clear_cache and cceverywhere_after_clear_cache hooks.
Example Usage
/**
* Add custom settings fields for purging a custom cache directory.
*
* This example adds a checkbox field that triggers a custom clearing action
* and a text field to define the directory URL.
*
* @param array $fields → An array of existing settings fields.
*
* @return array → The modified settings fields array, including any new custom fields.
*/
add_filter( 'cceverywhere_custom_settings', function( $fields ) {
// Add a custom checkbox field that also defines where the action is called
$fields[] = [
'key' => 'purge_custom_cache_directory', // A unique field key
'title' => __( 'Purge Custom Cache Directory', 'clear-cache-everywhere' ), // Field title
'type' => 'checkbox', // Field type
'sanitize' => 'sanitize_checkbox', // Sanitize callback
'section' => 'custom', // Section name in the settings - Always use "custom"
'default' => false, // Default value (unchecked)
'comments' => __( 'Purges our custom cache directory.', 'clear-cache-everywhere' ), // Description
'run_context' => 'ajax', // When and how the action is run (accepts "ajax" or "page" for page reload)
'callback' => 'myprefix_clear_custom_cache_directory'
];
// Add a custom text field that we can use in our callback method
$fields[] = [
'key' => 'custom_cache_directory_url', // A unique field key
'title' => __( 'Custom Cache Directory URL', 'clear-cache-everywhere' ),
'type' => 'text',
'sanitize' => 'sanitize_text_field',
'section' => 'custom', // Always use "custom",
'default' => WP_CONTENT_DIR . '/cache/myplugin/', // Default value
'comments' => __( 'Enter the URL of the custom cache directory to purge. If left blank, it will default to wp-content/cache/myplugin/', 'clear-cache-everywhere' ),
];
return $fields; // Return the modified settings fields
} );
/**
* The callback method for purging a custom cache directory.
* MUST be called correctly from the 'cceverywhere_custom_settings' hook above under 'callback' property.
*
* @return array Status of the clearing action.
*/
function myprefix_clear_custom_cache_directory() {
// The prefix for all settings
$prefix = 'clear_cache_everywhere_';
// Get custom directory URL from settings
$custom_dir = sanitize_text_field( get_option( $prefix . 'custom_cache_directory_url', '' ) );
// Default path if no custom URL is set
$cache_dir = ! empty( $custom_dir ) ? $custom_dir : WP_CONTENT_DIR . '/cache/myplugin/';
// Check if directory exists
if ( ! file_exists( $cache_dir ) ) {
return [
'status' => 'fail',
'error_message' => __( 'Cache directory does not exist.', 'clear-cache-everywhere' ),
];
}
// Attempt to delete files
$files = glob( $cache_dir . '*' );
$failed = [];
foreach ( $files as $file ) {
if ( ! @unlink( $file ) ) {
$failed[] = $file;
}
}
if ( ! empty( $failed ) ) {
return [
'status' => 'fail',
'error_message' => sprintf( __( 'Failed to delete %d file(s) in the cache directory.', 'clear-cache-everywhere' ), count( $failed ) ),
];
}
return [
'status' => 'success',
'error_message' => null,
];
}cceverywhere_before_clear
TYPE ⇢ Action
Added in version 1.2.0.
Fires immediately before each individual cache clearing action is executed. This replaces the now deprecated cceverywhere_before_clear_cache hook that only fired once before clearing all cache.
Example Usage
/**
* Hook into all each clearing action before it runs
*
* $action properties:
* - 'key' => string, unique action key
* - 'title' => string, human-readable label
* - 'callback' => callable|null, optional callback
*/
add_action( 'cceverywhere_before_clear', function( $action ) {
error_log( sprintf( 'About to clear cache action: %s', $action[ 'title' ] ) );
});cceverywhere_after_clear
TYPE ⇢ Action
Added in version 1.2.0.
Fires immediately after each individual cache clearing action is executed. This replaces the now deprecated cceverywhere_after_clear_cache hook that only fired once after clearing all cache.
Example Usage
/**
* Hook into all each clearing action after it runs
*
* $action properties:
* - 'key' => string, unique action key
* - 'title' => string, human-readable label
* - 'callback' => callable|null, optional callback
*
* $status => string, result status ('success', 'fail', 'skipped', 'info')
* $msg => string|null, additional information or error details about the action
*/
add_action( 'cceverywhere_after_clear', function( $action, $status, $msg ) {
if ( $status === 'fail' ) {
wp_mail( '[email protected]', 'Cache Clear Failed', 'Action: ' . $action[ 'title' ] . "\nError: " . $msg );
}
});cceverywhere_clearing_actions
TYPE ⇢ Filter
Filters the final list of clearing actions after the plugin has built it from the settings fields, including any added with cceverywhere_custom_settings. Use it to turn an action on or off in code, swap its callback, or remove it. The list is built once per request.
Each item in the array has these properties:
key: Unique action key.title: Human-readable label.run_context:'ajax'or'page'.callback: Callable ornullto use the built-in method.enabled: Whether the action is included in Clear All and the admin bar button.
Setting enabled only affects Clear All and the admin bar button. The action’s own Clear button still works. If you remove an item from the array, it can no longer be run at all, including from its own Clear button.
Example Usage
/**
* Never include Transients in Clear All or the admin bar button.
*
* @param array $actions → The list of clearing actions.
*
* @return array → The modified list of clearing actions.
*/
add_filter( 'cceverywhere_clearing_actions', function( $actions ) {
foreach ( $actions as &$action ) {
if ( 'transients' === $action[ 'key' ] ) {
$action[ 'enabled' ] = false;
}
}
unset( $action );
return $actions;
} );cceverywhere_before_clear_cache – deprecated
TYPE ⇢ Action
This action hook was removed in version 1.2.0.
All clearing operations have been moved to individual methods for greater control and more granular clearing behavior, and a new action hook (cceverywhere_before_clear) has been added to fire before each individual action instead of only once before performing all actions like this one did.
If you were previously using this hook to perform custom clearing actions, you should now use the cceverywhere_custom_settings hook.
That hook supports run_context and callback properties, allowing you to define exactly when and how your custom clearing logic executes.
cceverywhere_after_clear_cache – deprecated
TYPE ⇢ Action
This action hook was removed in version 1.2.0.
All clearing operations have been moved to individual methods for greater control and more granular clearing behavior, and a new action hook (cceverywhere_after_clear) has been added to fire after each individual action instead of only once after performing all actions like this one did.
If you were previously using this hook to perform custom clearing actions, you should now use the cceverywhere_custom_settings hook.
That hook supports run_context and callback properties, allowing you to define exactly when and how your custom clearing logic executes.
