How the WP Consent API Works

1. Privacy Law Compliance

The GDPR and similar laws require that consent must be obtained before setting non-essential cookies or processing personal data. WP Consent API helps you meet those requirements.

2. Standardized Behavior

Different plugins handling consent differently can cause conflicts or inconsistent behavior. The API unifies consent handling across the Wordpress site.

3. Simplifies Development

Wordpress plugin developers can use built-in API functions instead of rolling out their own consent logic.

The API allows developers and plugins to:

  • Check consent state before executing scripts
  • React to changes in consent dynamically

Example Use:

// Check if consent is given for 'analytics'
if (wp_has_consent('analytics')) {
  // Load analytics script
}
  • granted
  • denied
  • unknown

Wrap Third-Party Scripts

Before loading tools like Google Analytics or Facebook Pixel:

if (wp_has_consent('analytics')) {
  // Insert analytics code
}

or In your PHP code:

if (wp_has_consent('marketing')){
  //do marketing stuff
}