Clear Cache Everywhere

The Clear Cache Everywhere plugin has some useful hooks for developers to use to further enhance its functionality.

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

PHP
/**
 * 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

PHP
/**
 * 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

PHP
/**
 * 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

PHP
/**
 * 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( 'admin@example.com', 'Cache Clear Failed', 'Action: ' . $action[ 'title' ] . "\nError: " . $msg );
    }
});

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.

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.