0

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();
    }



}
  • PHP functions can only return 1 result, if you put 10 return statements, only the first one is ever executed – Tom J Nowell Jun 7 '15 at 16:23
3

get_the_utm_vars doesn't work like you think it does. A function cannot return more than one value. Right now, when the first return is reached, that value is returned and none of the following lines are ever reached.

You could have get_the_utm_vars return an array of values instead:

function get_the_utm_vars(){
    $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_campaign"] ); 

    return $utm;
}

Then you could call get_the_utm_vars to get all of your parameters.

$utm = get_the_utm_vars();
echo $utm['source']; // Will output $_GET["utm_source"]

Regarding your updated function. That could work as well. Whenever you need to access one of those variables within a function, you would need to import it into your scope using global.

function submit_contact_form() {
    global $utm_source, $utm_medium, $utm_term, $utm_content, $utm_campaign;

    // ...
}
  • both didn't work for me :( Are you sure it should work? – Kar19 Jun 8 '15 at 8:08
  • I edited my post with the current code parts for you to see :) ! – Kar19 Jun 8 '15 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.