Make WordPress plugins follow your cookie banner
TermsFeed Privacy Consent does not write to the WordPress Consent API by itself, so you add code that declares Privacy Consent, sends each answer, and restores it on every page. The WP Consent API is a WordPress plugin that gives other plugins one shared place to read consent. Plugins that support it follow the answer from your consent banner.
Only those plugins follow it. A plugin that ignores the API keeps adding its tracking for every visitor, so turn off its tracking output and add the service as a Privacy Consent vendor instead.
Setup takes 4 steps and about 20 minutes: install the plugin, map the vendor categories, send the answer, and restore it for returning visitors. You need administrator access, a child theme or a code snippets plugin for the PHP filter, and the Privacy Consent snippet in the <head>. Install the snippet first with Install TermsFeed Privacy Consent on WordPress.
Turn off other consent banners on WordPress
Deactivate every other consent plugin first, because a plugin that registers with the WP Consent API overwrites the answer Privacy Consent sends. This includes the Cookie Consent and Cookie Notice kits in TermsFeed AutoTerms. Then reload the site in a new private window, and confirm that only the Privacy Consent banner appears. Find each plugin to turn off in 2 Consent Banners Appear on a Site.
Step 1: Install the WP Consent API plugin
In the WordPress admin, open Plugins, click Add New Plugin, search for "WP Consent API", then install and activate it. The plugin adds the JavaScript functions wp_set_consent and wp_has_consent, which your code and other plugins call. To confirm, open the Console tab on your site and run typeof wp_set_consent, which returns "function".
Step 2: Declare Privacy Consent and map the vendor categories
Tell WordPress that the site asks for opt-in consent. With optin, a WP Consent API category counts as allowed only after the visitor allows it, which matches the denied start of every Privacy Consent integration. Add this PHP filter to the functions.php file of your child theme, or to a code snippets plugin.
add_filter( 'wp_get_consent_type', function () {
return 'optin';
} );
Then add this script to the <head>, above the Privacy Consent snippet, with the method you used to install Privacy Consent. functional is always allowed, preferences follows Functionality, both statistics categories follow Analytics, and marketing needs both Advertising and Marketing, as the category mapping sets.
<script>
window.wp_consent_type = 'optin';
document.dispatchEvent(new CustomEvent('wp_consent_type_defined'));
function tfWpConsent(categories) {
var has = function (id) { return categories.indexOf(id) !== -1; };
var ads = has('advertising') && has('marketing');
return {
'functional': 'allow',
'preferences': has('functionality') ? 'allow' : 'deny',
'statistics': has('analytics') ? 'allow' : 'deny',
'statistics-anonymous': has('analytics') ? 'allow' : 'deny',
'marketing': ads ? 'allow' : 'deny'
};
}
function tfSendToWordPress(categories) {
if (typeof window.wp_set_consent !== 'function') { return; }
var consent = tfWpConsent(categories);
Object.keys(consent).forEach(function (category) {
window.wp_set_consent(category, consent[category]);
});
}
</script>
Step 3: Send the answer from the Privacy Consent callbacks
Add this script after the previous one. acceptedAll and acceptedSome receive the vendor categories the visitor accepted in full, and rejectedAll receives nothing, so a partly accepted vendor category sends deny.
<script>
var callbacks = {
acceptedAll: function (categories) { tfSendToWordPress(categories); },
acceptedSome: function (categories) { tfSendToWordPress(categories); },
rejectedAll: function () { tfSendToWordPress([]); }
};
if (window.termsfeed_privacy_consent !== undefined)
window.termsfeed_privacy_consent.v1.callbacks(callbacks);
else
window.termsfeed_privacy_consent_callbacks = callbacks;
</script>
Each v1.callbacks() call replaces all 4 callbacks, so put the WordPress call in the same functions as any other integration. Merge them with the callback rules.
Step 4: Restore the answer for returning visitors
No answer callback runs on page load for a returning visitor, so this script sends the stored answer to the WP Consent API on every page load. It waits until the page finishes parsing, so the WP Consent API functions exist when it runs. It reads the termsfeed_privacy_consent_state key, counts a vendor category as accepted only when every vendor in it is on, and ignores an answer older than 365 days (12 months).
<script>
function tfStoredCategories() {
try {
var record = JSON.parse(localStorage.getItem('termsfeed_privacy_consent_state') || '{}');
var state = record.consentState;
if (!state || !state.consentedDate) { return null; }
if (Date.now() / 1000 - state.consentedDate > 365 * 24 * 60 * 60) { return null; }
return state.categories.filter(function (c) {
return c.vendors && c.vendors.length
? c.vendors.every(function (v) { return v.enabled; })
: c.enabled === true;
}).map(function (c) { return c.id; });
} catch (e) {
return null;
}
}
(function () {
function restore() {
var stored = tfStoredCategories();
if (stored) { tfSendToWordPress(stored); }
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', restore);
} else {
restore();
}
})();
</script>
A visitor on the opt-out model does not see a consent banner and has no stored answer. The code then sends nothing, and every WordPress category reads as not allowed. Those visitors run every Privacy Consent vendor while plugins that read the API hold back. Set "Consent logic" to "Strict" on the "Settings" page to ask every visitor.
How to verify that WordPress receives the Privacy Consent answer
Read the WordPress consent state in the Console tab after each answer. The wp_has_consent function returns true or false for a category. Purge any page cache first, so the site serves the new scripts.
- Open your site in a new private window, open DevTools, select the Network tab, and type
events.pushin the filter box. - Click I Agree. A new
events.pushrequest appears, which shows that Privacy Consent recorded the answer. - Select the Console tab and run
wp_has_consent('statistics')andwp_has_consent('marketing'). Both returntrue. - Load a second page and run the same commands. Both still return
true. - Close every private window, open a new one, load the site, click I Decline, and run the commands again. Both return
false.
A plugin that still loads its tracking after I Decline does not read the WP Consent API. Filter the Network tab for its host, then turn its tracking off and add the service as a vendor.
Common mistakes with the WordPress Consent API and Privacy Consent
2 systems sending Google Consent Mode v2 signals disagree. Site Kit by Google sends its own gtag('consent', ...) commands from the WP Consent API, and a Google template in Privacy Consent sends a second set. Keep one of them, or leave out the Google template and your own block from the Consent Mode steps.
A caching or optimization plugin that delays the head scripts breaks the order. An option that delays, defers, or combines JavaScript runs the Privacy Consent snippet and your callbacks after the tracking plugins. Exclude plugin.js from cdn.termsfeedtag.com and these inline scripts from every delay option, then purge the cache.