The "Callbacks" field on the Customization tab of the Cookie Consent kit holds a JavaScript object of TermsFeed Free Cookie Consent 4.2.0 callbacks. TermsFeed AutoTerms prints the object on every page of your WordPress website. Use it to send Google Consent Mode v2 updates or to start code once the visitor accepts a category.

The field takes the callback names of the Cookie Consent script as its keys, with a function as each value. Look up each callback, its arguments, and the moment it fires in Free Cookie Consent Callbacks and DOM Events.

How the plugin adds the callbacks to your WordPress website

The plugin prints the Callbacks as you wrote them, in a <script> of their own with the id termsfeed-autoterms-cookie-consent-callbacks, on every page with the kit on. Find the full Callbacks script, its JavaScript global, and the run script in Cookie Banner Code on WordPress Pages.

Make the Google Consent Mode v2 default command the first script in the <head> of every page, after only the charset meta. It goes above any Google tag, any Google Tag Manager container, and the Cookie Consent script. The plugin does not print Consent Mode defaults, and the Callbacks send only updates. The kit prints the vendor scripts in the footer, so the defaults come before them too. WordPress prints the cookie-consent.js tag of the kit at priority 9 of the wp_head action. Add the defaults from a wp_head action with priority 1, or above wp_head() in the header.php of a classic theme.

For the action, paste this code into a code snippets plugin or the functions.php file of a child theme. It prints the denied default of all 6 signals at priority 1.

add_action( 'wp_head', function () {
    ?>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('consent', 'default', {
        'ad_storage': 'denied',
        'ad_user_data': 'denied',
        'ad_personalization': 'denied',
        'analytics_storage': 'denied',
        'functionality_storage': 'denied',
        'personalization_storage': 'denied',
        'wait_for_update': 500
      });
    </script>
    <?php
}, 1 );

Check the order in the page source: the gtag('consent', 'default', ...) call is the first script in the <head>, above cookie-consent.js from www.termsfeed.com, any Google tag, and any Google Tag Manager container. Copy the default command and the full page layout from Step 1: Set every consent signal to denied before any Google tag.

Send Google Consent Mode v2 updates from a user_consent_saved callback, which sends granted or denied for every category on each answer and on each page load before an answer. A withdrawal then reaches the Google tags as soon as the visitor saves it, and the update arrives before the Cookie Consent script releases any vendor script. This object maps each category to its signals, and the typeof gtag check keeps the code silent on a page without the Google tag.

{
    'user_consent_saved': function (details) {
        if (typeof gtag !== 'function') {
            return;
        }
        gtag('consent', 'update', {
            'analytics_storage': details.tracking ? 'granted' : 'denied',
            'ad_storage': details.targeting ? 'granted' : 'denied',
            'ad_user_data': details.targeting ? 'granted' : 'denied',
            'ad_personalization': details.targeting ? 'granted' : 'denied',
            'functionality_storage': details.functionality ? 'granted' : 'denied',
            'personalization_storage': details.functionality ? 'granted' : 'denied'
        });
    }
}

On later page loads of a visitor who answered, the callback runs only while the Configuration Parameters set user_consent_saved_on_page_load to true. Without that key, Google tags on those pages keep the denied default. Read when the callback runs, and why it needs no callbacks_force, in Free Cookie Consent Callbacks and DOM Events.

Replace, save, and check your Callbacks on your WordPress website.

  1. Open the Customization tab of the Cookie Consent kit and replace the content of the "Callbacks" field with your object.
  2. Click the "Save Changes" button. The plugin checks the code first, without running it, and asks before saving code with a syntax error.
  3. Open the "Configuration Parameters" tab, add "user_consent_saved_on_page_load": true, as a new line after the opening { of the JSON, and click the "Save Changes" button.
  4. Clear your page cache.
  5. Close every private window, open your WordPress website in a new one, open the page source, and search for termsfeed-autoterms-cookie-consent-callbacks. Confirm that the script holds your object.

Test the Callbacks with the GDPR preference, which shows the "I decline" button, and with the Google Consent Mode v2 defaults in the <head>. Start each test except "Next page load" in a new private window, with your WordPress website and the Chrome DevTools Console tab open, after you close every other private window. Before each answer, run dataLayer.filter(e => e[0] === 'consent' && e[1] === 'update').length in the Console and note the count. After the answer, run it again and confirm that the count grew by 1. Replace .length with .at(-1) to read the state Google tags use in that newest update entry.

  • Partial acceptance. Click the "Change my preferences" button, turn on only the switch of the "Tracking cookies" tab, and click the "Save my preferences" button. The entry shows analytics_storage as granted and the 3 advertising signals as denied.
  • Refusal. Click the "I decline" button. The entry shows every signal as denied.
  • Withdrawal after acceptance. Click the "I agree" button, then the "Update cookies preferences" link, select the "Targeting and advertising cookies" tab, set its switch to Inactive, and click the "Save my preferences" button. Without a reload, the entry shows the 3 advertising signals as denied.
  • Next page load. Reload the page from the withdrawal test. A new update entry repeats the saved state.

To keep a consent record, add code that sends each answer to your server inside the same user_consent_saved callback. Copy that code from How to record each answer from user_consent_saved.

The "Callbacks" confirmation and error messages

On every save, the plugin warns when the Callbacks are not an object. Saving the Customization tab adds a compile check, and code that does not compile opens a confirmation dialog in the browser. The "Cancel" button stops the save and puts the cursor in the field. The "OK" button saves and prints the code, although the dialog says the Callbacks are left out. The browser stops the code with a SyntaxError until you correct it.

The Callbacks are not valid JavaScript (<error>). They are left out of your website until they are corrected. Save anyway?

Code that compiles but is not an object between { and } is saved with an error, and your WordPress website does not use the Callbacks until you correct it.

Callbacks must be a JavaScript object enclosed in { and }, for example: { 'notice_banner_loaded': () => {} }. They are not used on your website until they are corrected.

Both messages leave the consent banner and the vendor scripts working. Fix each message with the steps in Cookie Banner Save Errors in WordPress.

How the "Callbacks" field stays in sync with the "Configuration Parameters"

The "Callbacks" field and the callbacks key of the Configuration Parameters hold the same code, and a save on either tab updates the other. The JSON holds the code as a single escaped string, so edit the code in the "Callbacks" field, where it keeps its line breaks. Read every rule that keeps the 2 tabs in sync in Cookie Banner JSON Settings in WordPress.

On a save by a user without the WordPress unfiltered_html capability, the plugin keeps the stored Callbacks on both tabs. Ask a user who holds unfiltered_html to change the Callbacks, and find the error and who lacks the capability in Code or HTML Not Saving in WordPress.