简体   繁体   English

如何使用Javascript / jQuery在Wordpress主题模板上设置cookie,然后在另一个模板中显示cookie值(如果是isset)

[英]How do I set a cookie on a Wordpress theme template using Javascript/jQuery and then display the cookie value in a different template (if isset)

I have a Wordpress website which I set a cookie using the jQuery cookie plugin . 我有一个Wordpress网站,我使用jQuery cookie插件设置了一个cookie。

This is my JS code in the end of one of my wordpress landing pages which sets a cookie using Javascript/jQuery - AND WORKS: 这是我的一个wordpress登陆页面末尾的JS代码,它使用Javascript / jQuery设置一个cookie - AND WORKS:

<script type="text/javascript" src="https://example.com/wp-content/themes/themeeee-child/js/jquery.cookie.js"></script>
<script type="text/javascript">

// Parse the URL
var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

// Give the URL parameters variable names
var source   = getUrlParameter('utm_source');
var medium   = getUrlParameter('utm_medium');
var term     = getUrlParameter('utm_term');
var content  = getUrlParameter('utm_content');
var campaign = getUrlParameter('utm_campaign');

// Setting Cookie using jQuery 
if(jQuery.cookie('utm_source') == null || jQuery.cookie('utm_source') == "") {
  jQuery.cookie('utm_source', source); 
}
if(jQuery.cookie('utm_medium') == null || jQuery.cookie('utm_medium') == "") {
  jQuery.cookie('utm_medium', medium); 
}
if(jQuery.cookie('utm_campaign') == null || jQuery.cookie('utm_campaign') == "") {
  jQuery.cookie('utm_campaign', campaign); 
}
if(jQuery.cookie('utm_term') == null || jQuery.cookie('utm_term') == "") {
  jQuery.cookie('utm_term', term); 
}
if(jQuery.cookie('utm_content') == null || jQuery.cookie('utm_content') == "") {
  jQuery.cookie('utm_content', content); 
}

// Set a flag 
jQuery.cookie('coo_flag', 1, { expires : 365 }); 


</script>

在此输入图像描述

$_COOKIE['coo_flag'] is set to "1" now. $_COOKIE['coo_flag']现在设置为“1”。

On a total different page, at the bottom of the template file, I have this code part that checks using PHP if the $_COOKIE['coo_flag'] is set, and if is true, it fires a google pixel script: 在完全不同的页面上,在模板文件的底部,我有这个代码部分,如果设置了$_COOKIE['coo_flag']则使用PHP进行检查,如果为true,则会触发google像素脚本:

<?php 

  // session_start();
  if ( $_COOKIE["coo_flag"] ) {
    echo $_COOKIE["coo_flag"];
?>

<!-- Google Code for Contact Us Registration Conversion Page -->
<script type="text/javascript">
  /* <![CDATA[ */
  var google_conversion_id = 666;
  var google_conversion_language = "en";
  var google_conversion_format = "3";
  var google_conversion_color = "666";
  var google_conversion_label = "666";
  var google_remarketing_only = false;
  /* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/666/?label=666&amp;guid=ON&amp;script=0"/>
</div>
</noscript>

<?php 
  } else {
    echo "false";
     var_dump($_COOKIE["coo_flag"]);
  }
?>

and I keep getting this output result: 我一直得到这个输出结果:

falseNULL falseNULL

I have been trying to get it using JS too: 我一直试图使用JS来实现它:

<script type="text/javascript" src="https://yellowheadinc.com/wp-content/themes/sogo-child/js/jquery.cookie.js"></script>

<script type="text/javascript">

var cookieValue = jQuery.cookie("coo_flag");
console.log(cookieValue);

</script>

and got no output for console.log(cookieValue); 并没有为console.log(cookieValue);输出console.log(cookieValue);

Please help me find the proper way to get the cookie variable. 请帮我找到获取cookie变量的正确方法。

[01.02.2017]: More enlightenment : [01.02.2017]: 更多启示

  • A few days after saving the cookie for a year (view screenshot), I noticed it vanished from there ( and I didn't clear my cookies!!!! ). 保存cookie一年后的几天(查看截图),我注意到它从那里消失了( 我没有清除我的饼干!!!! )。

  • Running the page without the "if" statement and just echoing the cookie doesn't work - both on Chrome and Firefox. 在没有“if”语句的情况下运行页面并且只是回显cookie也无效 - 无论是在Chrome还是Firefox上。

  • An edit to @ Björn M : 编辑@ BjörnM

在此输入图像描述

Jquery Cookie creates cookies with a path in addition to the domain: Jquery Cookie除了域之外还创建具有路径的cookie:

path path: '/' Define the path where the cookie is valid. path path:'/'定义cookie有效的路径。 By default the path of the cookie is the path of the page where the cookie was created (standard browser behavior). 默认情况下,cookie的路径是创建cookie的页面的路径(标准浏览器行为)。 If you want to make it available for instance across the entire domain use path: '/'. 如果您希望在整个域中使用它,请使用路径:'/'。 Default: path of page where the cookie was created. 默认值:创建cookie的页面路径。

A cookie for path /abc is not visible to a page on path /def even though both pathes are on the same domain. 路径/ def上的页面看不到路径/ abc的cookie,即使两个路径都在同一个域上。 Giving the cookie the root path / as quoted above should do the trick. 给cookie提供上面引用的根路径应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM