Skip to content
Advertisement

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.

Advertisement

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' );
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement