如何在php插件中共享两个函数之间的变量?

时间:2014-10-06 09:50:55

标签: php wordpress plugins

我需要将变量(calltoaction)传递给函数2.我试过全局,它不会。怎么做?

功能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传递变量。当我提交下面显示的“process”函数时,调用并执行。但全球变量没有通过。

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

2 个答案:

答案 0 :(得分:0)

您可以使用$ GLOBALS var

$i=1;

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

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

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

希望它对你有帮助

答案 1 :(得分: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 */
}

因为你没有在所有函数之外设置(或者至少没有向我们展示你已设置)全局变量,所以函数不知道变量是什么。

另请注意,在函数内部使用“global”时,只需键入一次“global”,并在...之后使用逗号分隔的全局变量列表。