Back to Community

How to Block Third-Party Cookies and Scripts with Cookie Notice & Compliance for GDPR / CCPA

Content

Many WordPress users choose the 'Cookie Notice & Compliance for GDPR / CCPA' plugin to help their website comply with privacy regulations. A common challenge is managing third-party elements like Google Analytics, Facebook pixels, embedded YouTube videos, and social widgets that place cookies before a user has given their consent.

Why This Happens

By default, most plugins and themes load their scripts immediately when a page is visited. This means tracking cookies from services like Google Analytics or Facebook are often set before a user has a chance to accept or refuse the cookie policy, which may not align with the prior consent requirement of regulations like the GDPR.

The Solution: Using the Script Blocking Feature

The 'Cookie Notice & Compliance for GDPR / CCPA' plugin includes a feature designed specifically to handle this issue. It allows you to block specific scripts from loading until the user has provided consent. Here’s how to implement it:

  1. Identify the Scripts to Block: Compile a list of all the third-party scripts on your site that set non-necessary cookies. Common examples include Google Analytics, Facebook Pixel, Google Maps, and any embedded social media iframes.
  2. Locate the Script Blocking Settings: In your WordPress dashboard, navigate to Cookie Notice -> Compliance. Look for the sections labeled "Script Blocking" or similar. These are typically text areas where you can input JavaScript code.
  3. Add Your Scripts: You must remove the original scripts from your theme or other plugins. Then, paste those exact scripts into the appropriate "Header scripts" or "Body scripts" text areas within the Cookie Notice settings. Scripts placed here will be prevented from executing until the user clicks "Accept."
  4. Test Thoroughly: Always test your website in a private/incognito browser window. Click "Refuse" on the notice and use your browser's developer tools (Application or Storage tab) to verify that the targeted cookies are no longer being set.

Alternative Method: Using Conditional Code

For more advanced control, you can use the plugin's provided PHP functions to conditionally output content based on user consent. This is useful for blocking iframes or other HTML elements, not just scripts.

Example Code for a Custom Plugin or Theme:

if ( function_exists('cn_cookies_accepted') && cn_cookies_accepted() ) {
    // Output the content only if cookies are accepted
    echo '<iframe src="..."></iframe>';
}

This code checks if the user has accepted cookies before outputting an iframe, effectively preventing it from loading and setting cookies if they have refused.

Important Considerations

  • Manual Configuration: The plugin does not automatically detect and block cookies. You must manually identify and configure each script or element you need to block, as confirmed by user reports in the support forums.
  • Granular Consent: The free version of the plugin primarily operates on an "all-or-nothing" consent basis. If you require users to accept or refuse different categories of cookies individually (e.g., analytics vs. advertising), this level of granular control is not available in the basic version.
  • Testing is Crucial: After configuration, use tools like Cookiebot or manually inspect your browser's cookies to ensure your implementation is working correctly and achieving prior consent.

By properly utilizing the script blocking and conditional output features, you can configure the 'Cookie Notice & Compliance for GDPR / CCPA' plugin to effectively prevent third-party cookies from being placed without user consent, helping your site move closer to full compliance.

Related Support Threads Support