php – WordPress enqueue scripts not adding scripts-ThrowExceptions

Exception or error:

I’m trying to load a script into a WordPerss page, however it seems the enqueue script does not add anything.

I’m using the following script

function loadCC(){
    wp_enqueue_script('cookieConsentJS', 'https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js');
    wp_enqueue_style('cookieConsentCSS', 'https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css');

    wp_add_inline_script("CookieConsent", "window.addEventListener(\"load\", function(){
    window.cookieconsent.initialise({
        \"palette\": {
            \"popup\": {
                \"background\": \"#107fc9\"
            },
            \"button\": {
                \"background\": \"#ffffff\"
            }
        },
        \"position\": \"top\",
        \"static\": true,
        \"content\": {
            \"message\": \"".__("Deze website maakt gebruik van cookies om authenticatie, navigatie en andere functies te beheren. Door het gebruik van onze website, gaat u ermee akkoord dat we dit soort cookies plaatsen op uw apparaat.","bpm_cc")."\",
            \"dismiss\": \"".__("Ik begrijp het","bpm_cc")."\"
        },
        \"elements\": {
            \"messagelink\": \"<span id=\\\"cookieconsent:desc\\\" class=\\\"cc-message\\\">{{message}}</span>\",
            \"dismiss\": \"<a aria-label=\\\"dismiss cookie message\\\" role=button tabindex=\\\"0\\\" class=\\\"vc_gitem-link vc_btn3 vc_btn3-size-md vc_btn-white\\\">{{dismiss}}</a>\"}
        })
    });");
}

add_action( 'wp_enqueue_scripts', 'loadCC' );

Did I make a mistake somewhere? The code seems correct to me.

How to solve:

Check your theme header.php file. It should contains wp_head(); function between head html tags.

Answer:

Try to change this:

`wp_add_inline_script("CookieConsent", "window.addEventListener(\"load\", function(){
    window.cookieconsent.initialise({
    \"palette\": {
    \"popup\": {
    \"background\": \"#107fc9\"
            },`

in this, maybe you missed a bracket?

wp_add_inline_script("CookieConsentJS", "window.addEventListener(\"load\", function(){
window.cookieconsent.initialise({
\"palette\": {
\"popup\": {
\"background\": \"#107fc9\"
}
        },

Answer:

Ok after some debugging i found the error was in the wp_add_inline_script part,
the handle for this has to be the same as wp_enqueue_script (atlease after this change it works)

<?php
/**
* Plugin Name: Cookie Consent
* Version: 1
* Author: Jasper (Bottle Post Media)
*/
function loadCC(){
    wp_enqueue_script('CookieConsentJS', 'https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js');
    wp_enqueue_style('CookieConsentCSS', 'https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css');
    wp_add_inline_script("CookieConsentJS", "window.addEventListener(\"load\", function(){
    window.cookieconsent.initialise({
    \"palette\": {
    \"popup\": {
      \"background\": \"#107fc9\"
    },
    \"button\": {
      \"background\": \"#ffffff\"
    }
    },
    \"position\": \"top\",
    \"static\": true,
    \"content\": {
    \"message\": \"".__("Deze website maakt gebruik van cookies om authenticatie, navigatie en andere functies te beheren. Door het gebruik van onze website, gaat u ermee akkoord dat we dit soort cookies plaatsen op uw apparaat.","bpm_cc")."\",
    \"dismiss\": \"".__("Ik begrijp het","bpm_cc")."\"
    },
    \"elements\": {
    \"messagelink\": \"<span id=\\\"cookieconsent:desc\\\" class=\\\"cc-message\\\">{{message}}</span>\",
    \"dismiss\": \"<a aria-label=\\\"dismiss cookie message\\\" role=button tabindex=\\\"0\\\" class=\\\"vc_gitem-link vc_btn3 vc_btn3-size-md vc_btn-white\\\">{{dismiss}}</a>\"}})});");
}

add_action( 'wp_enqueue_scripts', 'loadCC' );

Leave a Reply

Your email address will not be published. Required fields are marked *