0

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.

5
  • Try changing wp_enqueue_style('wp_enqueue_scripts', to wp_enqueue_style('wp_enqueue_scripts2', . If it works it's a name conflict. Commented May 5, 2017 at 11:03
  • I copied the handles incorrectly they where suppose to be cookieConsentJS and cookieConsentCSS instead of wp_enqueue_scripts i will ament in the question, it however does not solve the problem Commented May 5, 2017 at 12:12
  • 1
    Any errors in console? Commented May 5, 2017 at 12:21
  • Try changing both something like that wp_enqueue_script('cookieConsentJS', 'https://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js', array(),'3.0.3', true); . 3.0.3 is the version, true means call it on footer (false to call it on header) , and not sure about array() Commented May 5, 2017 at 12:24
  • Also try using wp_register_script first for the external scripts..and after registering enqueue it Commented May 5, 2017 at 12:28

3 Answers 3

1

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' );
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.