如何将变量从函数.php中的一个函数传递到另一个函数

时间:2015-06-07 作者:Kar19

我有一个从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();
    }



}

1 个回复
SO网友:Mathew Tinsley

get_the_utm_vars 不像你想象的那样工作。函数不能返回多个值。现在,当达到第一个返回值时,将返回该值,并且不会达到以下任何一行。

你本来可以的get_the_utm_vars 而是返回一个值数组:

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;
}
然后你可以打电话get_the_utm_vars 获取所有参数。

$utm = get_the_utm_vars();
echo $utm[\'source\']; // Will output $_GET["utm_source"]
关于您的更新功能。这也可以奏效。每当需要访问函数中的其中一个变量时,都需要使用global.

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

    // ...
}

结束

相关推荐

需要通过Functions.Php注销脚本

嗯,我有点失望Wordpress注销脚本有多么困难。首先,我得到了所有句柄的列表,所以我查找了它,句柄是jquery migrate然后我将其添加到我的函数中。phpwp_dequeue_script(\'jquery-migrate\'); 还有这个wp_dequeue_script(\'jquery\'); 尽管脚本已正确注册,但它什么也不做。版本字符串出了什么问题,我想不出为什么它们仍然包含这些字符串,应该尽快在下一个WP版本中删除,它们只会在某些情况下阻止缓存正确缓存,这很烦人