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:
- 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.
- 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.
- 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."
- 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
-
Is your plugin realy GDPR compliant?https://wordpress.org/support/topic/is-your-plugin-realy-gdpr-compliant/
-
Where are the individual cookies settings ?https://wordpress.org/support/topic/where-are-the-individual-cookies-settings/
-
Cookie Plugin Limitationshttps://wordpress.org/support/topic/cookie-plugin-limitations/
-
Recaptchahttps://wordpress.org/support/topic/recaptcha-164/
-
Creating a shortcode to use in e.g. customizerhttps://wordpress.org/support/topic/creating-a-shortcode-to-use-in-e-g-customizer/
-
Custom cookies are not being blocked. Is this normal?https://wordpress.org/support/topic/custom-cookies-are-not-being-blocked-is-this-normal-2/
-
Feature: Content Blocker?https://wordpress.org/support/topic/feature-content-blocker/
-
Analytics cookies only set 3 pages laterhttps://wordpress.org/support/topic/analytics-cookies-only-set-3-pages-later/
-
Cookie info indexatedhttps://wordpress.org/support/topic/cookie-info-indexated/
-
Blocking your Math Captcha plugin cookieshttps://wordpress.org/support/topic/blocking-your-math-captcha-plugin-cookies/
-
cookie factory and adsense consenthttps://wordpress.org/support/topic/cookie-factory-and-adsense-consent/
-
How to GEO target cookie bar or its content?https://wordpress.org/support/topic/how-to-geo-target-cookie-bar-or-its-content/
-
A few questions not clear even in documentationhttps://wordpress.org/support/topic/a-few-questions-not-clear-even-in-documentation/
-
cookie opt-outhttps://wordpress.org/support/topic/cookie-opt-out/
-
2.4.14 New: Google Consent V2 support (Cookie Compliance only)https://wordpress.org/support/topic/2-4-14-new-google-consent-v2-support-cookie-compliance-only/
-
Cookie Notice and visitors counter.https://wordpress.org/support/topic/cookie-notice-and-visitors-counter/
-
Cross Domain Support?https://wordpress.org/support/topic/cross-domain-support/
-
Do Not Track Requestshttps://wordpress.org/support/topic/do-not-track-requests-2/
-
unique cookies for specific pageshttps://wordpress.org/support/topic/unique-cookies-for-specific-pages/
-
Cookie, page-reload and lost referrer: big problemhttps://wordpress.org/support/topic/cookie-page-reload-and-lost-referrer-big-problem/
-
¿This plugin is useful to comply with the gdpr?https://wordpress.org/support/topic/this-plugin-is-useful-to-comply-with-the-gdpr/
-
Cookies ajax loadinghttps://wordpress.org/support/topic/cookies-ajax-loading/
-
GDPR iframe blockinghttps://wordpress.org/support/topic/gdpr-iframe-blocking/
-
What type of cookie does this plugin use?https://wordpress.org/support/topic/what-type-of-cookie-does-this-plugin-use/
-
DNT signalshttps://wordpress.org/support/topic/dnt-signals/
-
GDPR & DSGVU Combination with other Pluginshttps://wordpress.org/support/topic/gdpr-dsgvu-combination-with-other-plugins/
-
Prior Consenthttps://wordpress.org/support/topic/prior-consent-2/
-
Cookie approval required!?https://wordpress.org/support/topic/cookie-approval-required/
-
guidence neededhttps://wordpress.org/support/topic/guidence-needed-3/
-
Does The Paid Version Detect All Cookies?https://wordpress.org/support/topic/does-the-paid-version-detect-all-cookies/
-
Geo-location limitations and other important questionshttps://wordpress.org/support/topic/geo-location-limitations-and-other-important-questions/
-
Is this pop-up window?https://wordpress.org/support/topic/is-this-pop-up-window/
-
Good Ideahttps://wordpress.org/support/topic/good-idea-68/
-
third party cookie integrationhttps://wordpress.org/support/topic/third-party-cookie-integration/
-
Cookie notice and MonsterInsightshttps://wordpress.org/support/topic/cookie-notice-and-monsterinsights/