【问题标题】:WordPress enqueue scripts not adding scriptsWordPress 排队脚本不添加脚本
【发布时间】:2020-03-17 22:35:44
【问题描述】:

我正在尝试将脚本加载到 WordPerss 页面中,但似乎 enqueue 脚本没有添加任何内容。

我正在使用以下脚本

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

我是不是在某个地方犯了错误?代码对我来说似乎是正确的。

【问题讨论】:

  • 尝试将 wp_enqueue_style('wp_enqueue_scripts', 更改为 wp_enqueue_style('wp_enqueue_scripts2', 。如果它有效,那就是名称冲突。
  • 我错误地复制了句柄,它们假设是 cookieConsentJS 和 cookieConsentCSS 而不是 wp_enqueue_scripts 我将在问题中进行修改,但它并没有解决问题
  • 控制台有错误吗?
  • 尝试同时更改类似 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 是版本号,true 表示在页脚调用(false 在页眉调用),不确定array()
  • 还可以先尝试将wp_register_script 用于external 脚本..并在注册后加入队列

标签: php wordpress


【解决方案1】:

好的,经过一些调试,我发现错误在 wp_add_inline_script 部分, 此句柄必须与 wp_enqueue_script 相同(至少在此更改后有效)

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

【讨论】:

    【解决方案2】:

    检查您的主题 header.php 文件。它应该在head html 标记之间包含wp_head(); 函数。

    【讨论】:

      【解决方案3】:

      尝试改变这一点:

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

      在这,也许你错过了一个括号?

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

      【讨论】:

        猜你喜欢
        • 2012-10-05
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        • 1970-01-01
        • 2021-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多