2014-10-06 84 views
0

我需要將變量(calltoaction)傳遞給函數2.我嘗試過全局,它不會。怎麼做?如何在php插件中的兩個函數之間共享變量?

功能1:

function wp_email_capture_form($calltoactiondesc,$error = 0) { 
    global $calltoaction; 
    /*Echo to a Variable*/ 
    ob_start(); 
    echo $calltoactiondesc; 
    $calltoaction= ob_get_contents(); 
    ob_end_clean(); 

    $url = get_option('home'); 
    $url = addLastCharacter($url); 
?> 
    <div id="wp_email_capture" class="wp-email-capture wp-email-capture-widget wp-email-capture-widget-worldwide"> 
     <form name="wp_email_capture" method="post" action="<?php echo $url; ?>"> 
      <?php if (isset($_GET['wp_email_capture_error'])) { 
       $error = wp_email_capture_sanitize($_GET['wp_email_capture_error']); 
       echo "<div class='wp-email-capture-error'>".__('Error:','WPEC'). $error ."</div>"; 
      } ?> 
      <div class="col-lg-9 col-sm-9 col-xs-9 col-md-9 form-group textbox-group"> 
       <input name="wp-email-capture-email" id="wp-email-capture-email-widget" type="text" class="wp-email-capture-email wp-email-capture-input wp-email-capture-widget-worldwide wp-email-capture-email-widget wp-email-capture-email-input wp-email-capture-email-input-widget form-control input-lg input" title="Email" placeholder="YOUR EMAIL ADDRESS" /> 
      </div> 
      <div class="col-lg-3 col-sm-3 col-xs-3 col-md-3 form-group textbox-group"> 
       <input type="hidden" name="wp_capture_action" value="1" /> 
       <input name="Submit" type="submit" value="<?php _e('SUBMIT','WPEC'); ?>" class="wp-email-capture-submit wp-email-capture-widget-worldwide subscribe-button" /> 
      </div> 
     </form> 
    </div> 
<?php 
} 

注:我只通過功能1.通過變量,當我在下面提交調用和執行所示的「處理」的功能。但是全球變量沒有通過。

function wp_email_capture_process() { 
    if(isset($_REQUEST['wp_capture_action'])) { 
    wp_email_capture_signup(); 
    } 
...... 
} 

功能2:

function wp_email_capture_signup() { 
    global $wpdb; 
    global $calltoaction; 
    // Random confirmation code 
    $confirm_code=md5(uniqid(rand())); 
    $starturl = $_SERVER['HTTP_REFERER']; 
    if (strpos($starturl, "?") === false) { 
    $extrastring = "?"; 
    } else { 
    $extrastring = "&"; 
    } 
    $email = $_REQUEST['wp-email-capture-email']; 
    if (!is_email($email)) { 
    $error = urlencode(__('Not a valid email','WPEC')); 
    $url = $starturl . $extrastring . "wp_email_capture_error=" . $error; 
    wp_redirect($url); 
    die(); 
    } 
.... 
.... 
} 

回答

0

您可以使用$ GLOBALS VAR

$i=1; 

function &test1 ($i) { 
    $GLOBALS['i']++; 
    return $GLOBALS['i']; 
} 

function test2(){ 
    echo $GLOBALS['i']; 
} 

test1($i); 
test2(); 
// echo $i : 2; 

希望這是你

+0

謝謝。但是,它可以」工作。 – Thangadurai 2014-10-06 11:32:55

0

爲了讓全局變量的工作,你需要幫助在所有函數之外聲明全局變量...

global $calltoaction; 

function wp_email_capture_form($calltoactiondesc,$error = 0) { 
    global $calltoaction; 
    /* the rest of your function */ 
} 

function wp_email_capture_process() { 
    /* the rest of your function */ 
} 

function wp_email_capture_signup() { 
    global $wpdb, 
      $calltoaction; 
    /* the rest of your function */ 
} 

因爲你沒有設置(或者至少沒有向我們顯示你已經設置了)所有函數之外的全局變量,函數不知道變量是什麼。

另外請注意,使用「全球」的功能裏面時,你只需要鍵入「全局」一次,並有後全局變量的逗號分隔列表...

相關問題