Home / Admin / Cache Exclude List
Duplicate Snippet

Embed Snippet on Your Site

Cache Exclude List

Exclude from cache using URL slug fragments.

Code Preview
php
<?php
// Cache Exclude List / Kiri Vadivelu / 2026
function cache_exclude_list() {
    // Array of slug fragments to exclude from cache
    $excluded_slugs = [
        '/red/',
        '/green/',
        '/blue/',
    ];
      // Ensure its a non-empty array
    if ( ! is_array( $excluded_slugs ) || empty( $excluded_slugs ) ) {
        return;
    }
    $uri = $_SERVER['REQUEST_URI'];
    foreach ( $excluded_slugs as $fragment ) {
        if ( str_contains( $uri, $fragment ) ) {
            if ( ! defined( 'DONOTCACHEPAGE' ) ) {
                define( 'DONOTCACHEPAGE', true );
            }
            return; // stop after first match
        }
    }
}
add_action( 'template_redirect', 'cache_exclude_list' ); 

Comments

Add a Comment