# Skip Page Caching for Visitors With a Cookie

> Makes page-cache plugins (WP Rocket, W3 Total Cache, WP Super Cache, LiteSpeed Cache and others that respect DONOTCACHEPAGE) skip caching for visitors who carry a cookie you choose, such as a personalisation, A/B test or affiliate cookie.

- Type: PHP
- Runs: Run everywhere
- Category: [Plugin Integrations](https://wpide.com/library/category/integrations/)
- Tags: cache, cookies, personalisation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cache-bypass-for-cookie/

## Code

```php
$wpide_cookie = 'wpide_variant'; // Cookie name (prefix match).

add_filter( 'rocket_cache_reject_cookies', function ( $cookies ) use ( $wpide_cookie ) {
    $cookies   = (array) $cookies;
    $cookies[] = $wpide_cookie;

    return array_values( array_unique( $cookies ) );
} );

add_action( 'init', function () use ( $wpide_cookie ) {
    if ( is_admin() || empty( $_COOKIE ) ) {
        return;
    }

    foreach ( array_keys( $_COOKIE ) as $name ) {
        if ( 0 === strpos( (string) $name, $wpide_cookie ) ) {
            if ( ! defined( 'DONOTCACHEPAGE' ) ) {
                define( 'DONOTCACHEPAGE', true );
            }
            if ( defined( 'LSCWP_V' ) ) {
                do_action( 'litespeed_control_set_nocache', 'wpide cookie' );
            }
            break;
        }
    }
}, 1 );
```

## Notes

Set $wpide_cookie. WP Rocket also gets the cookie added to its "never cache" list, so cached copies are not served to those visitors; re-save its settings once.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Skip Page Caching for Visitors With a Cookie" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
