JavaScript API
The plugin exposes a global window.mdccConsent object with the following methods:
Get Current Consent State
var state = mdccConsent.current();
// Returns: { analytics: true/false, ads: true/false }Accept All
mdccConsent.acceptAll();
// Sets analytics: true, ads: true
// Updates GCM v2 and dispatches change eventAccept Analytics Only
mdccConsent.acceptAnalyticsOnly();
// Sets analytics: true, ads: false
// Updates GCM v2 and dispatches change eventDecline All
mdccConsent.declineAll();
// Sets analytics: false, ads: false
// Updates GCM v2 and dispatches change eventReset Consent
mdccConsent.reset();
// Removes consent from localStorage
// User will see popup again on next page loadJavaScript Events
Listen for consent changes anywhere in your code:
document.addEventListener('mdcc:changed', function(event) {
var state = event.detail;
console.log('Analytics:', state.analytics);
console.log('Ads:', state.ads);
// Example: load a third-party script after consent
if (state.analytics) {
loadHotjar();
}
});The event bubbles from the document and includes the full consent state in event.detail.
localStorage Schema
Consent state is stored at the key mdcc_consent:
{
"analytics": true,
"ads": false
}Both fields are booleans. If the key doesn't exist or is malformed, the plugin defaults to { analytics: false, ads: false }.
Google Consent Mode Integration
The plugin calls gtag('consent', 'update', {...}) whenever consent changes:
gtag('consent', 'update', {
'analytics_storage': state.analytics ? 'granted' : 'denied',
'ad_storage': state.ads ? 'granted' : 'denied',
'ad_user_data': state.ads ? 'granted' : 'denied',
'ad_personalization': state.ads ? 'granted' : 'denied'
});If gtag is not available (no tracking code installed), the update is silently skipped.
Default Consent Injection
On wp_head at priority 1, the plugin injects:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'wait_for_update': 500
});
</script>This runs before GTM, GA4, or any other tracking script. The wait_for_update: 500 parameter tells Google tags to wait 500ms for a consent update before proceeding with the denied state.
PHP Architecture
Class Structure
wp_head priority 1) and frontend script enqueuing. Singleton pattern.wp_footer.[mdcc_consent_status] and [mdcc_manage_consent].Constants
MDCC_VERSION // Current plugin version (1.7.3)
MDCC_PLUGIN_FILE // Main plugin file path
MDCC_PLUGIN_DIR // Plugin directory path
MDCC_PLUGIN_URL // Plugin URL
MDCC_PLUGIN_BASENAME // Plugin basename for hooks
MDCC_TEXT_DOMAIN // 'maxtdesign-cookie-consent'Asset Loading
WordPress loads minified assets (.min.css, .min.js) by default. When SCRIPT_DEBUG is true in wp-config.php, unminified source files are loaded instead.
Asset handles:
mdcc-popup-css — Popup stylesmdcc-consent-runtime — Consent JavaScriptmdcc-admin-css — Admin styles (admin only)mdcc-admin-js — Admin JavaScript (admin only)Uninstall Behavior
When the plugin is deleted (not deactivated), all data is cleaned:
mdcc_settings, mdcc_versionmdcc_cacheClient-side localStorage data is not affected by uninstall.
Debug Mode
Enable WordPress debug mode to see console output:
// wp-config.php
define('WP_DEBUG', true);The plugin outputs [MDCC] prefixed messages to the browser console showing: