Back to Community

How to Conditionally Hide the Cookie Notice on Specific Pages

Content

One of the most common questions from users of the 'Cookie Notice & Compliance for GDPR / CCPA' plugin is how to prevent the banner from appearing on certain pages. This is often needed for pages like a Privacy Policy, a login page, an internal webview, or a kiosk mode display where the notice is unnecessary or disruptive to the user experience.

Why This Happens

By default, the plugin is designed to display the cookie consent banner site-wide to ensure compliance. The plugin's core functionality does not include a built-in, user-friendly interface for excluding specific pages or post types, which leads users to seek alternative methods for conditional display.

Common Solutions

1. Using the Conditional Display Setting (Limited)

Some versions of the plugin may offer a basic 'Conditional Display' option within its settings. This typically allows you to select a single page, often intended for a Privacy Policy, where the banner should be hidden.

Limitation: As seen in the sample threads, this built-in option can sometimes be unreliable, may not work with caching, and often only allows for one page to be specified. It may also not account for multilingual pages where each language has its own Privacy Policy page.

2. Using a Filter Hook (Recommended Method)

The most powerful and reliable method is to use the cn_cookie_notice_output filter hook provided by the plugin. This allows for complete programmatic control over when the notice is displayed.

Example 1: Hiding the notice on a specific page by ID

function my_hide_cookie_notice( $output ) {
    // Replace '123' with the ID of your target page
    if ( is_page( 123 ) ) {
        return '';
    }
    return $output;
}
add_filter( 'cn_cookie_notice_output', 'my_hide_cookie_notice' );

Example 2: Hiding the notice on multiple pages or conditions

function my_hide_cookie_notice( $output ) {
    // Hide on pages with these IDs
    if ( is_page( array( 123, 456, 789 ) ) ) {
        return '';
    }
    // Or hide on a custom post type
    if ( is_singular( 'my_custom_post_type' ) ) {
        return '';
    }
    // Or hide on the login page
    if ( is_login_page() ) { // Note: You may need to define this condition
        return '';
    }
    return $output;
}
add_filter( 'cn_cookie_notice_output', 'my_hide_cookie_notice' );

How to implement this code: You should add this code to your theme's functions.php file or, preferably, use a code snippets plugin to manage custom functions. Always clear any active cache on your site (plugin, server, or CDN like Cloudflare) after implementing such changes.

3. For Geolocation-Based Hiding

Many users ask about showing the notice only to visitors from specific regions like the EU. The plugin itself does not handle geolocation. As mentioned in the threads, this requires a separate plugin like 'Category Country Aware' (CCA) which can integrate with Cookie Notice to restrict its display based on the user's country.

Troubleshooting Tips

  • Caching: If your conditional rules are not taking effect, the most common culprit is a caching system. Purge all levels of cache (plugin, server, CDN).
  • Javascript Conflicts: In one thread, a user's 'Dismiss notice' button was broken due to a Javascript error. If your notice behaves strangely, check your browser's console for errors.
  • Check the Page ID: Ensure you are using the correct page ID in your conditional function. The ID in the WordPress admin may differ from what you expect.

By using the cn_cookie_notice_output filter, you gain fine-grained control to hide the cookie notice on any page or under any condition you can define with WordPress conditional tags.

Related Support Threads Support