在 Wordpress functions.php 中设置 cookie - 无法使用其他函数回显它
Setting a cookie in Wordpress functions.php - cant echo it using an other function
我的第一个函数从 URL 获取参数并设置 cookie:
编辑:
第一个函数在 WP 上的所有内容之前加载
并保存 cookie
在检查浏览器 cookie 时 - 它就在那里。它已保存。
/**
Get variables from the URL and set them into the coockie
**/
add_action( 'init', 'set_utm_cookie');
function set_utm_cookie() {
global $utm;
if (getenv('HTTP_CLIENT_IP'))
$utm['ip'] = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$utm['ip'] = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$utm['ip'] = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$utm['ip'] = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$utm['ip'] = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$utm['ip'] = getenv('REMOTE_ADDR');
else
$utm['ip'] = 'UNKNOWN';
$utm = array(
'utm_source' => htmlspecialchars( $_GET["utm_source"]),
'utm_medium' => htmlspecialchars( $_GET["utm_medium"]),
'utm_term' => htmlspecialchars( $_GET["utm_term"]),
'utm_content' => htmlspecialchars( $_GET["utm_content"]),
'utm_campaign' => htmlspecialchars( $_GET["utm_content"]),
'ip' => $utm['ip']
);
if ( !isset($_COOKIE['utm_source']) ) {
setcookie('utm_source', $utm['utm_source'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_medium']) ) {
setcookie('utm_medium', $utm['utm_medium'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_term']) ) {
setcookie('utm_term', $utm['utm_term'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_content']) ) {
setcookie('utm_content', $utm['utm_content'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_campaign']) ) {
setcookie('utm_campaign', $utm['utm_campaign'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['ip']) ) {
setcookie('ip', $utm['ip'], time()+3600*60*24*7);
}
}
第二个功能使用ajax激活
每当用户提交联系表单时 - 它会将输入发送到此功能并发送电子邮件。
如果电子邮件已发送(使用 wp_mail 函数 - 它会发送相同的电子邮件,但包含我保存在 cookie 中的内容。)
当我收到电子邮件时 - coolie 输出的字段为空白 :X 除了 IP。我得到了IP。
/**
Contact form using Ajax
**/
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
// Send information from the contact form
function submit_contact_form(){
// Get the UTM variables from the 'get_the_utm_vars()' function
//$utm = get_the_utm_vars();
// If there is a $_POST['email']...
if( isset($_POST['email']) && ($_POST['validation'] == true ) ) {
// Get parameters
$email = $_POST['email']; // Gets the email of the user..
$email_to = "arik@example.pro";
$walid = 'walid@example.pro';
$fullname = $_POST['fullname'];
$message = $_POST['text'];
$email_subject = "yellowHEAD Intro: $email";
$headers = array(
'From: '. $fullname .' <'. $email .'>',
'BCC: yonatan@example.pro',
'BCC: gal@example.pro',
'BCC: eran@example.pro',
'BCC: tova@example.pro',
'BCC: walid@example.pro',
'Content-type: text/html; charset=\"UTF-8\"; format=flowed \r\n'
);
// Send email to YH, and if sent - do:
if ( wp_mail($email_to,$email_subject,$message,$headers) ) {
// Tells me that the mail has been sent
echo json_encode( array("result"=>"complete") );
//Add the UTM variables to the emails text
$message .= "\r\n \r\n \r\n UTM Campaign: ".$_COOKIE['utm_campaign']." \r\n UTM Medium: ".$_COOKIE['utm_medium']." \r\n UTM Term: ".$_COOKIE['utm_term'] ."\r\n UTM Content: ".$_COOKIE['utm_content']." \r\n UTM Campaign: ".$_COOKIE['utm_campaign']." ";
// A mail for tova with the UTM paramseters
wp_mail($walid,$email_subject,$message);
// An automail for the user
//$message_responce = "Hi $fullname, \r\n Thanks for reaching out to us with your inquiry. We have received your e-mail and somebody from yellowHEAD will respond to you within one working day. \r\n In the meantime, feel free to connect with us through our Facebook or LinkedIn or to check out our professional blog. \r\n \r\n Talk to you soon. \r\n - The yellowHEAD Team";
//$header_responce = 'From: yellowHEADinc.com Team <info@exampleinc.com>';
//wp_mail($email,'yellowHEAD - Thanks For Your E-mail',$message_responce,$header_responce );
} else {
echo json_encode(array("result"=>"mail_error"));
var_dump($GLOBALS['phpmailer']->ErrorInfo);
}
wp_die();
}
}
如何正确回应这些 cookie?
我几乎可以肯定有一些我不知道的基本知识可以解决所有问题 :X
http://i.stack.imgur.com/ey6TH.png
尝试添加 cookie 路径和 cookie 域
setcookie('utm_campaign',$utm['utm_campaign'] , time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);
我的第一个函数从 URL 获取参数并设置 cookie:
编辑:
第一个函数在 WP 上的所有内容之前加载 并保存 cookie 在检查浏览器 cookie 时 - 它就在那里。它已保存。
/**
Get variables from the URL and set them into the coockie
**/
add_action( 'init', 'set_utm_cookie');
function set_utm_cookie() {
global $utm;
if (getenv('HTTP_CLIENT_IP'))
$utm['ip'] = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$utm['ip'] = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$utm['ip'] = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$utm['ip'] = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$utm['ip'] = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$utm['ip'] = getenv('REMOTE_ADDR');
else
$utm['ip'] = 'UNKNOWN';
$utm = array(
'utm_source' => htmlspecialchars( $_GET["utm_source"]),
'utm_medium' => htmlspecialchars( $_GET["utm_medium"]),
'utm_term' => htmlspecialchars( $_GET["utm_term"]),
'utm_content' => htmlspecialchars( $_GET["utm_content"]),
'utm_campaign' => htmlspecialchars( $_GET["utm_content"]),
'ip' => $utm['ip']
);
if ( !isset($_COOKIE['utm_source']) ) {
setcookie('utm_source', $utm['utm_source'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_medium']) ) {
setcookie('utm_medium', $utm['utm_medium'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_term']) ) {
setcookie('utm_term', $utm['utm_term'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_content']) ) {
setcookie('utm_content', $utm['utm_content'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['utm_campaign']) ) {
setcookie('utm_campaign', $utm['utm_campaign'], time()+3600*60*24*7);
}
if ( !isset($_COOKIE['ip']) ) {
setcookie('ip', $utm['ip'], time()+3600*60*24*7);
}
}
第二个功能使用ajax激活 每当用户提交联系表单时 - 它会将输入发送到此功能并发送电子邮件。 如果电子邮件已发送(使用 wp_mail 函数 - 它会发送相同的电子邮件,但包含我保存在 cookie 中的内容。)
当我收到电子邮件时 - coolie 输出的字段为空白 :X 除了 IP。我得到了IP。
/**
Contact form using Ajax
**/
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
// Send information from the contact form
function submit_contact_form(){
// Get the UTM variables from the 'get_the_utm_vars()' function
//$utm = get_the_utm_vars();
// If there is a $_POST['email']...
if( isset($_POST['email']) && ($_POST['validation'] == true ) ) {
// Get parameters
$email = $_POST['email']; // Gets the email of the user..
$email_to = "arik@example.pro";
$walid = 'walid@example.pro';
$fullname = $_POST['fullname'];
$message = $_POST['text'];
$email_subject = "yellowHEAD Intro: $email";
$headers = array(
'From: '. $fullname .' <'. $email .'>',
'BCC: yonatan@example.pro',
'BCC: gal@example.pro',
'BCC: eran@example.pro',
'BCC: tova@example.pro',
'BCC: walid@example.pro',
'Content-type: text/html; charset=\"UTF-8\"; format=flowed \r\n'
);
// Send email to YH, and if sent - do:
if ( wp_mail($email_to,$email_subject,$message,$headers) ) {
// Tells me that the mail has been sent
echo json_encode( array("result"=>"complete") );
//Add the UTM variables to the emails text
$message .= "\r\n \r\n \r\n UTM Campaign: ".$_COOKIE['utm_campaign']." \r\n UTM Medium: ".$_COOKIE['utm_medium']." \r\n UTM Term: ".$_COOKIE['utm_term'] ."\r\n UTM Content: ".$_COOKIE['utm_content']." \r\n UTM Campaign: ".$_COOKIE['utm_campaign']." ";
// A mail for tova with the UTM paramseters
wp_mail($walid,$email_subject,$message);
// An automail for the user
//$message_responce = "Hi $fullname, \r\n Thanks for reaching out to us with your inquiry. We have received your e-mail and somebody from yellowHEAD will respond to you within one working day. \r\n In the meantime, feel free to connect with us through our Facebook or LinkedIn or to check out our professional blog. \r\n \r\n Talk to you soon. \r\n - The yellowHEAD Team";
//$header_responce = 'From: yellowHEADinc.com Team <info@exampleinc.com>';
//wp_mail($email,'yellowHEAD - Thanks For Your E-mail',$message_responce,$header_responce );
} else {
echo json_encode(array("result"=>"mail_error"));
var_dump($GLOBALS['phpmailer']->ErrorInfo);
}
wp_die();
}
}
如何正确回应这些 cookie? 我几乎可以肯定有一些我不知道的基本知识可以解决所有问题 :X
http://i.stack.imgur.com/ey6TH.png
尝试添加 cookie 路径和 cookie 域
setcookie('utm_campaign',$utm['utm_campaign'] , time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false);