Jasper B
Jasper B

Reputation: 11

WordPress enqueue scripts not adding scripts

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.

Upvotes: 0

Views: 429

Answers (3)

Jasper B
Jasper B

Reputation: 11

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' );

Upvotes: 1

user7889803
user7889803

Reputation:

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\"
}
        },

Upvotes: 0

Kulikov Sergey
Kulikov Sergey

Reputation: 265

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

Upvotes: 0

Related Questions