Hope someone can help me : )
What Im trying to do is, get the first visit referrer and send it to form. We user Contact form 7 plugin, so I created a hidden input, get the referrer URL and assign that value of referrer to that hidden input field.
Everything was working fine, till I enabled the caching.
This is the function I use (in case anyone will need it)
function getRefererPage( $form_tag ){
if(!isset($_COOKIE['external_referrer_cookie']))
{
}
if ( $form_tag['name'] == 'referer-page' ) {
$form_tag['values'][] = $_COOKIE['external_referrer_cookie'];
}
return $form_tag;
}
add_filter( 'wpcf7_form_tag', 'getRefererPage' );
Where "referer-page" is the name and ID of the hidden field in Contact Form 7 form.
Once I enabled cache, I had to think of a Javascript of jQuery solution to set the cookie.
I added jQuery Cookie Plugin () and added then tried multiple different variations of the code below to create a cookie and set the cookie value.
My Goal is: When website visitor comes to first time, set the cookie value to the referrer url, but then keep the value, for example if visitor comes to website from google and then moves around the pages on website, once he fills the form, the value in the hidden field still should be the google url with its parameters.
This is the JS code I have in the footer.php of theme now.
jQuery(document).ready(function($){ // BEGIN CUSTOM JQUERY NO CONFLICT
// $.cookie('mysite_referrer', document.referrer);
// var initreferrer = document.referrer;
// $.cookie('mysite_referrer', initreferrer);
//console.log('current cookie ref:'+$.cookie('external_referrer_cookie'));
//console.log('testing');
$.cookie('external_referrer_cookie');
if ($.cookie('external_referrer_cookie') == 'undefined' ) {
// console.log('setting cookie for'+ document.referrer);
$.cookie('external_referrer_cookie', document.referrer);
$('.wpcf7-text.referer-page').val($.cookie('wsg_external_referrer_cookie'));
console.log('current cookie ref:'+$.cookie('wsg_external_referrer_cookie'));
}
// $('#referer-page').val($.cookie('external_referrer_cookie'));
console.log('current cookie out of function ref:'+$.cookie('external_referrer_cookie'));
/*
var cookieName = 'cookietest';
$(function() {
checkCookie();
});
function checkCookie() {
if (document.cookie.length > 0 && document.cookie.indexOf(cookieName + '=') != -1) {
// do nothing, cookie already sent
} else {
// handle jQuery animation
// set the cookie to show user has already visited
document.cookie = cookieName + "=1";
}
}
*/
}); // END CUSTOM JQUERY NO CONFLICT
As you can see in commented out lines, I tried lots of approaches I found in several website tutorials. Ive been struggling on this for couple days now, please help : ))
Thank You
Hope someone can help me : )
What Im trying to do is, get the first visit referrer and send it to form. We user Contact form 7 plugin, so I created a hidden input, get the referrer URL and assign that value of referrer to that hidden input field.
Everything was working fine, till I enabled the caching.
This is the function I use (in case anyone will need it)
function getRefererPage( $form_tag ){
if(!isset($_COOKIE['external_referrer_cookie']))
{
}
if ( $form_tag['name'] == 'referer-page' ) {
$form_tag['values'][] = $_COOKIE['external_referrer_cookie'];
}
return $form_tag;
}
add_filter( 'wpcf7_form_tag', 'getRefererPage' );
Where "referer-page" is the name and ID of the hidden field in Contact Form 7 form.
Once I enabled cache, I had to think of a Javascript of jQuery solution to set the cookie.
I added jQuery Cookie Plugin (https://github/carhartl/jquery-cookie) and added then tried multiple different variations of the code below to create a cookie and set the cookie value.
My Goal is: When website visitor comes to first time, set the cookie value to the referrer url, but then keep the value, for example if visitor comes to website from google and then moves around the pages on website, once he fills the form, the value in the hidden field still should be the google url with its parameters.
This is the JS code I have in the footer.php of theme now.
jQuery(document).ready(function($){ // BEGIN CUSTOM JQUERY NO CONFLICT
// $.cookie('mysite_referrer', document.referrer);
// var initreferrer = document.referrer;
// $.cookie('mysite_referrer', initreferrer);
//console.log('current cookie ref:'+$.cookie('external_referrer_cookie'));
//console.log('testing');
$.cookie('external_referrer_cookie');
if ($.cookie('external_referrer_cookie') == 'undefined' ) {
// console.log('setting cookie for'+ document.referrer);
$.cookie('external_referrer_cookie', document.referrer);
$('.wpcf7-text.referer-page').val($.cookie('wsg_external_referrer_cookie'));
console.log('current cookie ref:'+$.cookie('wsg_external_referrer_cookie'));
}
// $('#referer-page').val($.cookie('external_referrer_cookie'));
console.log('current cookie out of function ref:'+$.cookie('external_referrer_cookie'));
/*
var cookieName = 'cookietest';
$(function() {
checkCookie();
});
function checkCookie() {
if (document.cookie.length > 0 && document.cookie.indexOf(cookieName + '=') != -1) {
// do nothing, cookie already sent
} else {
// handle jQuery animation
// set the cookie to show user has already visited
document.cookie = cookieName + "=1";
}
}
*/
}); // END CUSTOM JQUERY NO CONFLICT
As you can see in commented out lines, I tried lots of approaches I found in several website tutorials. Ive been struggling on this for couple days now, please help : ))
Thank You
Share Improve this question edited Mar 24, 2016 at 17:10 Jevuska 1,17010 silver badges21 bronze badges asked Mar 24, 2016 at 16:48 DomenikoDomeniko 481 gold badge1 silver badge9 bronze badges 1- It look like you use older version jQuery Cookie Plugin, as the page note This project was moved to github/js-cookie/js-cookie so maybe you need to update your code again. – Jevuska Commented Mar 24, 2016 at 19:55
1 Answer
Reset to default 2I don't have the answer build on cookie, but I think this isn't sensitive data, you can try localStorage.
jQuery(document).ready(function($){
var initreferrer = document.referrer;
if(initreferrer.indexOf('yourdomain') === -1 ) { // Check if the referer is your site or not. If not( return -1 ) set the localStorage.
localStorage.setItem("mysite_referrer", initreferrer);
}
});
Then you can use a javascript function (instead of a filter like you did) to fill your hidden field with the referer saved in browser localStorage. We can get it by localStorage.getItem("mystite_referer")
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745601557a4635430.html
评论列表(0条)