How to Check User Cookie Consent Status in PHP with GDPR Cookie Compliance
Content
If you're developing custom functionality for your WordPress site, you may need to check a user's cookie consent status before executing certain code. This is a common requirement for developers integrating third-party services, custom tracking, or dynamic content that should only load after a user has given their explicit permission.
Why You Need to Check Consent Status
The core principle of regulations like GDPR is that user consent must be obtained before any non-essential processing of their data. This means your custom code, which might set cookies or load external scripts, should not run unless the user has opted in. Relying solely on the GDPR Cookie Compliance plugin's front-end blocking is often not enough for scripts you control directly in your theme or custom plugins.
The Solution: Using Built-in PHP Hooks
The 'GDPR Cookie Compliance' plugin provides specific PHP functions to check the consent status for each category. You can use these conditional checks within your theme's functions.php file or a custom plugin.
Here are the primary hooks available:
- Check Strictly Necessary Cookies:
gdpr_cookie_is_accepted( 'strict' ) - Check Third Party Cookies:
gdpr_cookie_is_accepted( 'thirdparty' ) - Check Advanced Cookies:
gdpr_cookie_is_accepted( 'advanced' )
These functions return a boolean value (true if accepted, false if not accepted).
Implementation Example
The following code snippet demonstrates how to conditionally load a script based on the user's consent for Third Party cookies.
// Example: Load a custom script only if Third Party cookies are accepted
if ( function_exists('gdpr_cookie_is_accepted') ) {
if ( gdpr_cookie_is_accepted( 'thirdparty' ) ) {
// Your code to enqueue a script or render content goes here
wp_enqueue_script( 'my-custom-tracking-script' );
}
}
Important Consideration: Force Reload
As noted in the support threads, a crucial step is often missed. To ensure these checks work correctly for all visitors and that content is not visible before consent is given, you must enable a 'force reload'. This can be done by adding the following line to your theme's functions.php file:
add_filter( 'gdpr_force_reload', '__return_true' );
This filter ensures the page reloads after consent is given or revoked, allowing your PHP checks to run again with the updated user preference.
Checking Consent in JavaScript
For front-end manipulations, the plugin also exposes JavaScript variables. You can check the status of consents using:
gdpr_consent__strictgdpr_consent__thirdpartygdpr_consent__advanced- A combined variable:
gdpr_consent__cookies
By leveraging these built-in tools, developers can ensure their custom code remains compliant with privacy regulations and respects the user's choices made through the cookie banner.
Related Support Threads Support
-
Almost perfect!https://wordpress.org/support/topic/almost-perfect-353/
-
How to check cookie consent on GDPR Cookie Compliance from different pluginhttps://wordpress.org/support/topic/how-to-check-cookie-consent-on-gdpr-cookie-compliance-from-different-plugin/
-
Ask users for a new consent (after Priv. Policy updates)https://wordpress.org/support/topic/ask-users-for-a-new-consent-after-priv-policy-updates/
-
Can you add another tabhttps://wordpress.org/support/topic/can-you-add-another-tab/
-
css class to show to anyone who does not consenthttps://wordpress.org/support/topic/css-class-to-show-to-anyone-who-does-not-consent/
-
Order of “Accept” and “Reject” buttonshttps://wordpress.org/support/topic/order-of-accept-and-reject-buttons/
-
withdraw view consentshttps://wordpress.org/support/topic/withdraw-view-consents/
-
Allow to set domain name (or set it as “.example.com”)https://wordpress.org/support/topic/allow-to-set-domain-name-or-set-it-as-example-com/
-
Any php code snippet to show the cookie bar?https://wordpress.org/support/topic/any-php-code-snippet-to-show-the-cookie-bar/
-
More cookie categoryhttps://wordpress.org/support/topic/more-cookie-category/
-
Cookie bar that always appears when you access the sitehttps://wordpress.org/support/topic/cookie-bar-that-always-appears-when-you-access-the-site/
-
Feature request: Automatic cookie declarationhttps://wordpress.org/support/topic/feature-request-automatic-cookie-declaration/
-
Feature request: Let 3rd part cookies be enabled before accepthttps://wordpress.org/support/topic/feature-request-let-3rd-part-cookies-be-enabled-before-accept/
-
Add cookie bar on amp pagehttps://wordpress.org/support/topic/add-cookie-bar-on-amp-page/
-
Does it possible a shortcode to list the cookies ?https://wordpress.org/support/topic/does-it-possible-a-shortcode-to-list-the-cookies/
-
Scan and categorize cookieshttps://wordpress.org/support/topic/scan-and-categorize-cookies/
-
Great Plugin But Please Take This Suggestionshttps://wordpress.org/support/topic/great-plugin-but-please-take-this-suggestions/
-
Some features missing!!https://wordpress.org/support/topic/some-features-missing-3/
-
Consider accepted cookies also by scrolling the page or opening some links?https://wordpress.org/support/topic/consider-accepted-cookies-also-by-scrolling-the-page-or-opening-some-links/
-
This plugin is incorrect in terms of GDPR compliancehttps://wordpress.org/support/topic/this-plugin-is-incorrect-in-terms-of-gdpr-compliance/
-
Filter to disable all notifications?https://wordpress.org/support/topic/filter-to-disable-all-notifications/
-
does the cookie bar pop up every time a user goes to the site?https://wordpress.org/support/topic/does-the-cookie-bar-pop-up-every-time-a-user-goes-to-the-site/
-
Plugin on Safarihttps://wordpress.org/support/topic/plugin-on-safari/
-
change cookie message according to regionhttps://wordpress.org/support/topic/change-cookie-message-according-to-region/
-
Exclude/Include Countries or Activate on Scroll Possible?https://wordpress.org/support/topic/exclude-include-countries-or-activate-on-scroll-possible/
-
Please enable acceptance when scrollinghttps://wordpress.org/support/topic/please-enable-acceptance-when-scrolling/
-
disable cookie popuphttps://wordpress.org/support/topic/disable-cookie-popup/
-
Move scrollable Tab Content below accept decline buttonhttps://wordpress.org/support/topic/move-scrollable-tab-content-below-accept-decline-button/
-
Enabled by default but only in effect/working after acceptance?https://wordpress.org/support/topic/enabled-by-default-but-only-in-effect-working-after-acceptance/
-
Check Compliance User Status from PHPhttps://wordpress.org/support/topic/howto-check-compliance-user-status-from-php/
-
New privacy Ruleshttps://wordpress.org/support/topic/new-privacy-rules/
-
Check when cookie tabs switch is changed?https://wordpress.org/support/topic/check-when-cookie-tabs-switch-is-changed/
-
Does your plugin save “acceptance” data? How?https://wordpress.org/support/topic/does-your-plugin-save-acceptance-data-how/
-
Add more than one cookie per categoryhttps://wordpress.org/support/topic/add-more-than-one-cookie-per-category/
-
Widget for profile pageshttps://wordpress.org/support/topic/widget-for-profile-pages/
-
Trigger Additional Cookies When Click on Closehttps://wordpress.org/support/topic/trigger-additional-cookies-when-click-on-close/
-
Accept cookies on clickhttps://wordpress.org/support/topic/accept-cookies-on-click/
-
Accept cookies on scrollhttps://wordpress.org/support/topic/accept-cookies-on-scroll/
-
Cookie Message in Widget Areahttps://wordpress.org/support/topic/cookie-message-in-widget-area/
-
different cookie banner for two regionshttps://wordpress.org/support/topic/different-cookie-banner-for-two-regions/
-
Add-on Mozilla 32-bit bughttps://wordpress.org/support/topic/add-on-mozilla-32-bit-bug/
-
Value of gdpr_cookie_popuphttps://wordpress.org/support/topic/value-of-gdpr_cookie_popup/
-
Please add body classeshttps://wordpress.org/support/topic/please-add-body-classes/
-
Request: Button size and strickly neccesary cookie alerthttps://wordpress.org/support/topic/button-size-and-stricky-neccesary-cookie/
-
Multisite?https://wordpress.org/support/topic/multisite-594/
-
Upgrading to pro and historyhttps://wordpress.org/support/topic/upgrading-to-pro-and-history/
-
Presale Question: Cookie Analysishttps://wordpress.org/support/topic/presale-question-cookie-analysis/