I have a function that gets the variables from the 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;
}
and I have a function that get the contact form inputs:
/**
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 = "arik@example.pro";
$walid = 'walid@example.pro';
$fullname = $_POST['fullname'];
$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'
);
$group_emails = array(
'info@example.com',
'gal@example.com',
'yonatan@example.com',
'arik@example.pro',
'tova@example.pro'
);
$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();
}
}
I want to send them in the email. Currently I don't get the variables - What is the right way to do this?
- Both functions are located in functions.php
Edit:
I have found this: https://stackoverflow.com/questions/18517705/pass-php-variable-created-in-one-function-to-another
I edited my function to:
/**
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"] );
}
it's not working for me.
Edit #2 for @mtinsley:
I used var_dump($utm); in the first function just to make sure it gets the params, and it works btw.
/**
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 = "arik@example.pro";
$walid = 'walid@example.pro';
$fullname = $_POST['fullname'];
$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'
);
$group_emails = array(
'info@example.com',
'gal@example.com',
'yonatan@example.com',
'arik@example.pro',
'tova@example.pro'
);
$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();
}
}