【发布时间】: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脚本..并在注册后加入队列