我有一个从URL(UTM)获取变量的函数:
/**
Get Parameters From UTM
**/
add_action(\'template_redirect\', \'get_the_utm_vars\'); // \'template_redirect\' - action hook that fires before rendering the template
function get_the_utm_vars(){
$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_campaign"] );
return $utm_source;
return $utm_medium;
return $utm_term;
return $utm_content;
return $utm_campaign;
}
我有一个函数可以获取联系人表单输入:
/**
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(){
// If there is a $_POST[\'email\']...
if( isset($_POST[\'email\']) && ($_POST[\'validation\'] == true ) ) {
$email = $_POST[\'email\'];
$email_to = "[email protected]";
$walid = \'[email protected]\';
$fullname = $_POST[\'fullname\'];
$headers = array(
\'From: \'. $fullname .\' <\'. $email .\'>\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'Content-type: text/html; charset=\\"UTF-8\\"; format=flowed \\r\\n\'
);
$group_emails = array(
\'[email protected]\',
\'[email protected]\',
\'[email protected]\',
\'[email protected]\',
\'[email protected]\'
);
$email_subject = "yellowHEAD Intro: $email";
$message = $_POST[\'text\'];
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: $utm_campaign \\r\\n ";
// Auto mail to the user
wp_mail($walid,$email_subject,$message);
} else {
echo json_encode(array("result"=>"mail_error"));
var_dump($GLOBALS[\'phpmailer\']->ErrorInfo);
}
wp_die();
}
}
我想通过电子邮件发送他们。目前我还没有得到这些变量,正确的方法是什么?
这两个函数都位于函数中。phpEdit:
我发现:https://stackoverflow.com/questions/18517705/pass-php-variable-created-in-one-function-to-another
我将我的函数编辑为:
/**
Get Parameters From UTM
**/
add_action(\'template_redirect\', \'get_the_utm_vars\'); // \'template_redirect\' - action hook that fires before rendering the template
function get_the_utm_vars(){
global $utm_source;
global $utm_medium;
global $utm_term;
global $utm_content;
global $utm_campaign;
$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_campaign"] );
}
这对我没用。
Edit #2 for @mtinsley:
我使用var\\u dump($utm);在第一个函数中,只是为了确保它获得参数,顺便说一句,它可以工作。
/**
Get Parameters From UTM
**/
add_action(\'template_redirect\', \'get_the_utm_vars\'); // \'template_redirect\' - action hook that fires before rendering the template
function get_the_utm_vars(){
global $utm;
$utm = array();
$utm[\'utm_source\'] = htmlspecialchars( $_GET["utm_source"] );
$utm[\'utm_medium\'] = htmlspecialchars( $_GET["utm_medium"] );
$utm[\'utm_term\'] = htmlspecialchars( $_GET["utm_term"] );
$utm[\'utm_content\'] = htmlspecialchars( $_GET["utm_content"] );
$utm[\'utm_campaign\'] = htmlspecialchars( $_GET["utm_campaign"] );
return $utm;
}
/**
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(){
$utm = get_the_utm_vars();
// If there is a $_POST[\'email\']...
if( isset($_POST[\'email\']) && ($_POST[\'validation\'] == true ) ) {
$email = $_POST[\'email\'];
$email_to = "[email protected]";
$walid = \'[email protected]\';
$fullname = $_POST[\'fullname\'];
$headers = array(
\'From: \'. $fullname .\' <\'. $email .\'>\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'BCC: [email protected]\',
\'Content-type: text/html; charset=\\"UTF-8\\"; format=flowed \\r\\n\'
);
$group_emails = array(
\'[email protected]\',
\'[email protected]\',
\'[email protected]\',
\'[email protected]\',
\'[email protected]\'
);
$email_subject = "yellowHEAD Intro: $email";
$message = $_POST[\'text\'];
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: ".$utm[\'utm_campaign\']." \\r\\n UTM Medium: ".$utm[\'utm_medium\']." \\r\\n UTM Term: ".$utm[\'utm_term\'] ."\\r\\n UTM Content: ".$utm[\'utm_content\']." \\r\\n UTM Campaign: ".$utm[\'utm_campaign\']." ";
// Automail to the user
wp_mail($walid,$email_subject,$message);
} else {
echo json_encode(array("result"=>"mail_error"));
var_dump($GLOBALS[\'phpmailer\']->ErrorInfo);
}
wp_die();
}
}