Back to Community

How to Check User Cookie Consent Status in PHP with GDPR Cookie Compliance

47 threads Sep 16, 2025

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__strict
  • gdpr_consent__thirdparty
  • gdpr_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