Custom Flags
While built-in flags cover common cases, custom flags let you target cache clearing based on your specific content structure and business logic.
Adding Custom Flags#Copied!
Via Filter#Copied!
The millicache_flags_for_request filter runs when a page is being cached:
1add_filter( 'millicache_flags_for_request', function( $flags ) {
2 // Add a flag based on the page template
3 if ( is_page_template( 'templates/landing.php' ) ) {
4 $flags[] = 'template:landing';
5 }
6
7 // Add a flag for pages with a specific Gutenberg block
8 if ( is_singular() && has_block( 'my-plugin/hero-banner' ) ) {
9 $flags[] = 'block:hero-banner';
10 }
11
12 return $flags;
13} );
Via PHP Function#Copied!
Add flags dynamically during template rendering:
1// In your theme's template file
2if ( is_product() ) {
3 millicache_add_flag( 'woo:product' );
4 millicache_add_flag( 'woo:product:' . get_the_ID() );
5}
6
7// Based on custom logic
8if ( get_field( 'show_pricing_table' ) ) {
9 millicache_add_flag( 'feature:pricing' );
10}
Via Rule Action#Copied!
Use MilliRules for condition-based flag assignment:
1use MilliCacheDepsMilliRulesRules;
2
3Rules::create( 'mysite:seasonal-flag' )
4 ->on( 'template_redirect', 25 )
5 ->when()
6 ->has_term( 'seasonal', 'product_cat' )
7 ->then()
8 ->add_flag( 'promo:seasonal' )
9 ->register();
Learn more about the powerful Rules System.
Removing Flags#Copied!
Sometimes you need to remove a built-in flag:
1// Via PHP function
2if ( is_front_page() && get_option( 'custom_homepage' ) ) {
3 millicache_remove_flag( 'home' );
4}
1// Via rule
2Rules::create( 'mysite:no-archive-flag' )
3 ->on( 'template_redirect', 30 )
4 ->when()
5 ->is_post_type_archive( 'product' )
6 ->then()
7 ->remove_flag( 'archive:post' )
8 ->register();
Clearing Cache by Flags#Copied!
WP-CLI#Copied!
1# Clear by specific flag
2wp millicache clear --flag="home"
3
4# Clear multiple flags
5wp millicache clear --flag="post:123,home,archive:post"
6
7# Clear with wildcard
8wp millicache clear --flag="post:*"
9wp millicache clear --flag="archive:category:*"
10
11# Multisite: Include site prefix
12wp millicache clear --flag="2:post:*"
13wp millicache clear --flag="*:home"
PHP Functions#Copied!
1// Clear by flags
2millicache_clear_cache_by_flags( ['home', 'archive:post'] );
3
4// Clear with wildcard
5millicache_clear_cache_by_flags( 'product:*' );
6
7// Expire instead of delete (serves stale while regenerating)
8millicache_clear_cache_by_flags( 'home', true );
9
10// Mixed targets (flags, post IDs, URLs)
11millicache_clear_cache( [
12 'home', // Flag
13 'post:123', // Flag
14 123, // Post ID
15 'https://example.com/special-page/' // URL
16] );
Wildcard Patterns#Copied!
MilliCache supports wildcards for flexible cache clearing:
The * Wildcard#Copied!
Matches any number of characters:
| Pattern | Matches |
|---|---|
post:* |
post:1, post:123, post:999 |
archive:* |
archive:post, archive:category:5 |
*:home |
1:home, 2:home (multisite) |
feature:* |
All feature flags |
The ? Wildcard#Copied!
Matches exactly one character:
| Pattern | Matches |
|---|---|
post:? |
post:1 through post:9 only |
?:home |
Sites with single-digit IDs |
Flag Design Patterns#Copied!
Hierarchical Flags#Copied!
Use a consistent naming structure for granular control:
1// E-commerce example
2$flags[] = 'product'; // All products
3$flags[] = 'product:category:5'; // Products in category 5
4$flags[] = 'product:5:sku:ABC123'; // Specific product variant
5
6// Clear all products
7millicache_clear_cache_by_flags( 'product:*' );
8
9// Clear category only
10millicache_clear_cache_by_flags( 'product:category:5' );
Feature Flags#Copied!
Tag pages by feature for cross-cutting concerns:
1add_filter( 'millicache_flags_for_request', function( $flags ) {
2 // Tag pages showing dynamic pricing
3 if ( has_dynamic_pricing() ) {
4 $flags[] = 'feature:dynamic-pricing';
5 }
6
7 // Tag pages with real-time inventory
8 if ( shows_inventory() ) {
9 $flags[] = 'feature:inventory';
10 }
11
12 return $flags;
13} );
14
15// When pricing engine updates, clear all affected pages
16millicache_clear_cache_by_flags( 'feature:dynamic-pricing' );
WooCommerce Integration#Copied!
1add_filter( 'millicache_flags_for_request', function( $flags ) {
2 if ( function_exists( 'is_product' ) && is_product() ) {
3 $product = wc_get_product();
4
5 // Tag by product type
6 $flags[] = 'woo:' . $product->get_type();
7
8 // Tag if on sale
9 if ( $product->is_on_sale() ) {
10 $flags[] = 'woo:sale';
11 }
12
13 // Tag by category
14 foreach ( $product->get_category_ids() as $cat_id ) {
15 $flags[] = 'woo:cat:' . $cat_id;
16 }
17 }
18 return $flags;
19} );
20
21// Clear all sale items when sale ends
22millicache_clear_cache_by_flags( 'woo:sale' );
Time-Based Flags#Copied!
For scheduled cache clearing:
1add_filter( 'millicache_flags_for_request', function( $flags ) {
2 // Add a date-based flag
3 $flags[] = 'date:' . date( 'Y-m-d' );
4
5 // Add a week flag for weekly content
6 $flags[] = 'week:' . date( 'Y-W' );
7
8 return $flags;
9} );
10
11// Clear yesterday's cached content via cron
12$yesterday = date( 'Y-m-d', strtotime( '-1 day' ) );
13millicache_clear_cache_by_flags( "date:{$yesterday}" );
Best Practices#Copied!
Use Descriptive Names#Copied!
1// Good
2$flags[] = 'product:featured';
3$flags[] = 'archive:sale';
4
5// Avoid
6$flags[] = 'x';
7$flags[] = '123';
Limit Flag Count#Copied!
Each flag adds storage overhead. Be selective:
1// Good: Few targeted flags
2$flags[] = 'post:' . $post->ID;
3$flags[] = 'archive:' . $post->post_type;
4
5// Avoid: Excessive flags
6foreach ( get_all_meta( $post->ID ) as $key => $value ) {
7 $flags[] = "meta:{$key}:{$value}"; // Could be hundreds!
8}
Use Wildcards for Clearing#Copied!
Instead of tracking exact flags, use patterns:
1// Good: Use wildcard
2millicache_clear_cache_by_flags( 'product:*' );
3
4// Avoid: Listing every flag
5millicache_clear_cache_by_flags( ['product:1', 'product:2', 'product:3', ...] );
Document Your Flag Taxonomy#Copied!
Plan and document your flag structure:
Your Site's Flag Structure:
├── home - Homepage
├── post:{id} - Individual posts
├── archive:
│ ├── post - Post archive
│ ├── {type} - CPT archives
│ └── {tax}:{id} - Taxonomy archives
├── product:
│ ├── featured - Featured products
│ └── sale - On-sale products
├── block:
│ ├── hero - Pages with hero block
│ └── testimonials - Pages with testimonials
└── feature:
├── pricing - Dynamic pricing pages
└── inventory - Real-time inventory
Next Steps#Copied!
- Cache Clearing — All clearing methods
- Rules Overview — Condition-based caching
- Hooks & Filters — All flag-related hooks