Hooks & Filters
This is a complete reference of all hooks and filters available in MilliCache.
Action Hooks#Copied!
Cache Storage Events#Copied!
millicache_entry_storing
Fires before a cache entry is stored.
1add_action( 'millicache_entry_storing', function( $hash, $key, $flags, $data ) {
2 // $hash - Cache hash
3 // $key - Cache key
4 // $flags - Array of flags
5 // $data - Cache data array
6
7 error_log( "Storing cache: $key with " . count( $flags ) . " flags" );
8}, 10, 4 );
millicache_entry_stored
Fires after a cache entry is stored.
1add_action( 'millicache_entry_stored', function( $hash, $key, $flags, $data ) {
2 // Notify external CDN
3 cdn_notify_cached( $key );
4}, 10, 4 );
millicache_entry_deleting
Fires before a cache entry is deleted.
1add_action( 'millicache_entry_deleting', function( $hash, $key, $flags ) {
2 error_log( "Deleting cache: $key" );
3}, 10, 3 );
millicache_entry_deleted
Fires after a cache entry is deleted.
1add_action( 'millicache_entry_deleted', function( $hash, $key, $flags ) {
2 // Notify external systems
3 cdn_notify_purged( $key );
4}, 10, 3 );
Cache Clearing Events#Copied!
millicache_cache_cleared_by_posts
Fires after cache is cleared by post IDs.
1add_action( 'millicache_cache_cleared_by_posts', function( $post_ids, $expire ) {
2 // $post_ids - Array of post IDs
3 // $expire - Whether expire mode was used
4
5 foreach ( $post_ids as $post_id ) {
6 error_log( "Cleared cache for post: $post_id" );
7 }
8}, 10, 2 );
millicache_cache_cleared_by_flags
Fires after cache is cleared by flags.
1add_action( 'millicache_cache_cleared_by_flags', function( $flags, $expire ) {
2 // $flags - Array of cleared flags
3 // $expire - Whether expire mode was used
4
5 // Notify external cache
6 external_cache_purge_by_tags( $flags );
7}, 10, 2 );
millicache_cache_cleared_by_sites
Fires after cache is cleared by site IDs (multisite).
1add_action( 'millicache_cache_cleared_by_sites', function( $site_ids, $network_id, $expire ) {
2 // $site_ids - Array of site IDs
3 // $network_id - Network ID (if specified)
4 // $expire - Whether expire mode was used
5}, 10, 3 );
millicache_cleared_by_networks
Fires after cache is cleared by network IDs.
1add_action( 'millicache_cleared_by_networks', function( $network_ids, $expire ) {
2 // $network_ids - Array of network IDs
3 // $expire - Whether expire mode was used
4}, 10, 2 );
millicache_cache_cleared
Fires after any cache clearing operation.
1add_action( 'millicache_cache_cleared', function( $expire ) {
2 // $expire - Whether expire mode was used
3
4 // Log clearing event
5 error_log( 'MilliCache cleared at ' . current_time( 'mysql' ) );
6
7 // Clear external caches
8 if ( function_exists( 'wp_cache_flush' ) ) {
9 wp_cache_flush();
10 }
11} );
REST API Events#Copied!
millicache_rest_cache_action_performed
Fires after a REST API cache action is performed.
1add_action( 'millicache_rest_cache_action_performed', function( $action, $params, $request ) {
2 // $action - Action performed (clear, clear_current, clear_targets)
3 // $params - Action parameters
4 // $request - WP_REST_Request object
5
6 // Audit log
7 audit_log( "Cache action: $action by user " . get_current_user_id() );
8}, 10, 3 );
millicache_rest_settings_action_performed
Fires after a REST API settings action is performed.
1add_action( 'millicache_rest_settings_action_performed', function( $action, $params, $request ) {
2 // $action - Action performed (reset, restore)
3
4 audit_log( "Settings action: $action" );
5}, 10, 3 );
Filter Hooks#Copied!
Settings Filters#Copied!
millicache_settings_defaults
Filter default settings.
1add_filter( 'millicache_settings_defaults', function( $defaults ) {
2 // Modify defaults
3 $defaults['cache']['ttl'] = 3600; // 1 hour default
4
5 return $defaults;
6} );
Cache Clearing Filters#Copied!
millicache_flags_related_to_post
Filter flags that should be cleared when a post is updated.
1add_filter( 'millicache_flags_related_to_post', function( $flags, $post ) {
2 // Add custom flags based on post content
3 if ( has_block( 'myblock/featured', $post ) ) {
4 $flags[] = 'featured-content';
5 }
6
7 // Add taxonomy-based flags
8 $categories = get_the_category( $post->ID );
9 foreach ( $categories as $cat ) {
10 $flags[] = 'category:' . $cat->term_id;
11 }
12
13 return $flags;
14}, 10, 2 );
millicache_settings_clear_site_hooks
Filter hooks that trigger full site cache clearing.
1add_filter( 'millicache_settings_clear_site_hooks', function( $hooks ) {
2 // Add a custom hook that should clear the cache
3 $hooks[] = 'my_custom_global_update';
4
5 // Remove a default hook
6 $key = array_search( 'switch_theme', $hooks );
7 if ( $key !== false ) {
8 unset( $hooks[ $key ] );
9 }
10
11 return $hooks;
12} );
Default hooks:
save_post_wp_template_partcustomize_save_afterwp_update_nav_menuswitch_themeupdate_option_permalink_structureupdate_option_active_plugins
millicache_settings_clear_site_options
Filter options that trigger full site cache clearing when updated.
1add_filter( 'millicache_settings_clear_site_options', function( $options ) {
2 // Add a custom option
3 $options[] = 'my_global_setting';
4
5 return $options;
6} );
Request Flags Filters#Copied!
millicache_flags_for_request
Filter flags assigned to the current request.
1add_filter( 'millicache_flags_for_request', function( $flags ) {
2 // Add flags based on content
3 if ( has_block( 'myblock/weather' ) ) {
4 $flags[] = 'block:weather';
5 }
6
7 // Add flags for WooCommerce
8 if ( function_exists( 'is_shop' ) && is_shop() ) {
9 $flags[] = 'woo:shop';
10 }
11
12 // Add a template-based flag
13 if ( is_page_template( 'templates/landing.php' ) ) {
14 $flags[] = 'template:landing';
15 }
16
17 return $flags;
18} );
Capability Filters#Copied!
millicache_clear_cache_capability
Filter the capability required to clear cache.
1add_filter( 'millicache_clear_cache_capability', function( $capability ) {
2 // Require higher capability
3 return 'manage_options'; // Only administrators
4
5 // Or lower for specific sites
6 // return 'edit_posts'; // Editors can clear
7} );
Default: publish_pages
REST API Filters#Copied!
millicache_rest_cache_allowed_actions
Filter allowed cache actions via REST API.
1add_filter( 'millicache_rest_cache_allowed_actions', function( $actions ) {
2 // Add custom action
3 $actions[] = 'my_custom_action';
4
5 // Remove an action
6 $key = array_search( 'clear', $actions );
7 if ( $key !== false ) {
8 unset( $actions[ $key ] );
9 }
10
11 return $actions;
12} );
Default: ['clear', 'clear_current', 'clear_targets']
millicache_rest_settings_allowed_actions
Filter allowed settings actions via REST API.
1add_filter( 'millicache_rest_settings_allowed_actions', function( $actions ) {
2 // Remove restore action
3 $key = array_search( 'restore', $actions );
4 if ( $key !== false ) {
5 unset( $actions[ $key ] );
6 }
7
8 return $actions;
9} );
Default: ['reset', 'restore']
millicache_rest_status_response
Filter the REST API status response.
1add_filter( 'millicache_rest_status_response', function( $status ) {
2 // Add custom data
3 $status['custom_metric'] = get_custom_cache_metric();
4
5 // Remove sensitive data
6 unset( $status['storage']['password'] );
7
8 return $status;
9} );
Hook Usage Examples#Copied!
Notify CDN on Cache Clear#Copied!
1add_action( 'millicache_cache_cleared_by_flags', function( $flags, $expire ) {
2 // Convert MilliCache flags to CDN tags
3 $cdn_tags = array_map( function( $flag ) {
4 return 'millicache-' . sanitize_title( $flag );
5 }, $flags );
6
7 // Purge CDN
8 cdn_purge_by_tags( $cdn_tags );
9}, 10, 2 );
Audit Logging#Copied!
1add_action( 'millicache_cache_cleared', function( $expire ) {
2 $user = wp_get_current_user();
3 $method = $expire ? 'expired' : 'deleted';
4
5 log_audit_event( 'cache_cleared', [
6 'user_id' => $user->ID,
7 'user_name' => $user->user_login,
8 'method' => $method,
9 'timestamp' => current_time( 'mysql' ),
10 ] );
11} );
Custom Flags for ACF#Copied!
1add_filter( 'millicache_flags_for_request', function( $flags ) {
2 if ( ! function_exists( 'get_field' ) ) {
3 return $flags;
4 }
5
6 // Add a flag for pages with a specific ACF field
7 if ( is_singular() && get_field( 'enable_dynamic_content' ) ) {
8 $flags[] = 'acf:dynamic';
9 }
10
11 return $flags;
12} );
13
14// Clear when the ACF field changes
15add_action( 'acf/save_post', function( $post_id ) {
16 if ( get_field( 'enable_dynamic_content', $post_id ) ) {
17 millicache_clear_cache_by_flags( [ 'acf:dynamic' ] );
18 }
19} );
Restrict Cache Clearing by Role#Copied!
1add_filter( 'millicache_clear_cache_capability', function( $capability ) {
2 // Only allow on staging/development
3 if ( defined( 'WP_ENV' ) && WP_ENV === 'production' ) {
4 return 'manage_options'; // Admins only in production
5 }
6
7 return 'edit_posts'; // Editors can clear on staging
8} );
Next Steps#Copied!
- API Reference - Function documentation
- Architecture - Internal structure
- Rules Introduction - Extend the rules engine