2014-03-06 30 views
0

需要WP插件的选项页面,用户可以通过从下拉列表中选择来删除链接。我需要在函数之间使用一个PHP变量

选项页面被创建并且变量被设置在一个名为printSelection()的函数中。该函数返回WP仪表板下拉框中的值。

function printSelection() { 
if(isset($_POST['selectbox'])){ 
    return $_POST['selectbox']; 
} 
} 

$link = printSelection(); 
print($link); 

$ link变量正在成功打印到WP选项页面。我想要做的就是在前端使用这个变量,就在表单之后。完整的代码是在这里:

<?php 
/* 
Plugin Name: Gladstone Brookes Mortgage Calculator Widget 
Plugin URI: http://gladstonebrookesmortgages.co.uk 
Description: A simple mortgage calculator widget 
Version: 1.0 
Author: Ian Butler 
Author URI: http://gladstonebrookesmortgages.co.uk 
*/ 

/*-----------------------------------------------------------------------------------*/ 
/* Include CSS */ 
/*-----------------------------------------------------------------------------------*/ 

function gb_mortgage_calculator_css() {  
wp_enqueue_style('gb_mortgage_calculator', plugins_url('assets/style.css', __FILE__), false, '1.0'); 
} 
add_action('wp_print_styles', 'gb_mortgage_calculator_css'); 

/*-----------------------------------------------------------------------------------*/ 
/* Include JS */ 
/*-----------------------------------------------------------------------------------*/ 

function gb_mortgage_calculator_scripts() { 
wp_enqueue_script('calc', plugins_url('assets/calculator.js', __FILE__), array('jquery'), '1.0', true); 
} 
add_action('wp_enqueue_scripts', 'gb_mortgage_calculator_scripts'); 

/*-----------------------------------------------------------------------------------*/ 
/* Register Widget */ 
/*-----------------------------------------------------------------------------------*/ 

class gb_mortgage_calculator extends WP_Widget { 

function gb_mortgage_calculator() { 
    $widget_ops = array('description' => 'Display a mortgage calculator.'); 
    parent::WP_Widget(false, __('GB Mortgage Calculator', 'gladstonebrookes'),$widget_ops);  
} 

function widget($args, $instance) { 
     extract($args); 
     $title = $instance['title']; 


    echo $before_widget; 
    if ($title) { echo $before_title . $title . $after_title; } 
     global $ct_options; 
    ?> 

<div id="calculator" class="grid-60 prefix-15 suffix-15">   
<form name="mortgageCalculator" id="mortgageCalculator"> 
    <label class="grid-60">Loan Amount (£):</label><input class="grid-40" id="la" type="text" name="la" value="0" /> 
    <label class="grid-60">Interest Rate (%):</label><input class="grid-40" id="ir" type="text" name="ir" value="0" /> 
    <label class="grid-60">Mortgage Term (Years):</label><input class="grid-40" id="mt" type="text" name="term" value="0" /> 
    <select id="type"><option id="r" value="repayment">Repayment</option> <option id="io" value="interestOnly">Interest Only</option></select>&nbsp; 
    <input onclick="checkForZero(this); calculatePayment(this)" type="button" name="cmdCalc" value="Calculate" /> 
    <input onclick="resetForm(this)" type="button" name="reset" value="Clear Form" /> 
    <label class="grid-60 bold">Total Repayable:</label><input class="grid-40 bold" id="payments" type="text" name="payments" /> 
    <label class="grid-60 bold">Monthly Payments:</label><input class="grid-40 bold" id="pmt" type="text" name="pmt" /> 
</form> 
<p><?php echo $link ?></p> 
</div> 

<div id="overlay" onclick="modal(this)"> 
<h4>Please enter numeric values only</h4> 
<h4>The value of the following fields cannot be zero:</h4> 
<p><strong>Loan Amount</strong></p><p><strong>Interest Rate</strong></p> 
<p><strong>Mortgage Term</strong></p> 
<p style="text-decoration:underline; color:blue;">dismiss</p> 
</div> 

<div id="fade"></div> 

<?php echo $after_widget; ?> 
<?php 
} 

function update($new_instance, $old_instance) {  return $new_instance; 
} 

function form($instance) { 

     $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; 

?> 
    <p> 
     <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','gladstonebrookes'); ?></label> 
     <input type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" /> 
    </p> 
    <?php 
} 
} 

add_action('widgets_init', create_function('', 'register_widget("gb_mortgage_calculator");')); 

/*-----------------------------------------------------------------------------------*/ 
/* Register Shortcode */ 
/*-----------------------------------------------------------------------------------*/ 

function gb_mortgage_calculator_shortcode($atts) { ?> 
    <div class="clear"></div> 

<div id="calculator" class="grid-60 prefix-15 suffix-15"> 
<h2>Mortgage Calculator</h2>   
<form name="mortgageCalculator" id="mortgageCalculator"> 
    <label class="grid-60">Loan Amount (£):</label><input class="grid-40" id="la" type="text" name="la" value="0" /> 
    <label class="grid-60">Interest Rate (%):</label><input class="grid-40" id="ir" type="text" name="ir" value="0" /> 
    <label class="grid-60">Mortgage Term (Years):</label><input class="grid-40" id="mt" type="text" name="term" value="0" /> 
    <select id="type"><option id="r" value="repayment">Repayment</option> 
    <option id="io" value="interestOnly">Interest Only</option></select>&nbsp; 
    <input onclick="checkForZero(this); calculatePayment(this)" type="button" name="cmdCalc" value="Calculate" /> 
    <input onclick="resetForm(this)" type="button" name="reset" value="Clear Form" /> 
    <label class="grid-60 bold">Total Repayable:</label><input class="grid-40 bold" id="payments" type="text" name="payments" /> 
    <label class="grid-60 bold">Monthly Payments:</label><input class="grid-40 bold" id="pmt" type="text" name="pmt" /> 
</form> 
<p><?php echo $link ?></p> 
</div> 

<div id="overlay" onclick="modal(this)"> 
<h4>Please enter numeric values only</h4><h4>The value of the following fields cannot be zero:</h4> 
<p><strong>Loan Amount</strong></p><p><strong>Interest Rate</strong></p> 
<p><strong>Mortgage Term</strong></p> 
<p style="text-decoration:underline; color:blue;">dismiss</p> 
</div> 

<div id="fade"></div> 

<?php } 
add_shortcode('mortgage_calculator', 'gb_mortgage_calculator_shortcode'); 

add_action('admin_menu', 'create_menu'); 

/*-----------------------------------------------------------------------------------*/ 
/* Create Options Page */ 
/*-----------------------------------------------------------------------------------*/ 


function create_menu(){ 

add_management_page('GB Mortgage Calculator', 'GB Mortgage Calculator', 10, 'gbmc_setting_file', 'gbmc_setting'); 

} 

function gbmc_setting() { ?> 

<div class="wrap"> 

<form method="post" name="options" target="_self"> 

    <h2>Display Link</h2> 

    <table width="100%" cellpadding="10" class="form-table"> 

    <tr> 
    <td align="left" scope="row"> 

     <label>Display Link</label> 
     <select name="selectbox"> 
      <option value='<a href="http://gladstonebrookesmortgages.co.uk">Powered by Gladstone Brookes Mortgages</a>'>block</option> 
      <option value="">hide</option> 
     </select> 

    </td> 
    </tr> 

    </table> 
    <p class="submit"> 

    <input type="submit" name="Submit" value="Update" /> 

    </p> 

</form> 

</div> 

<?php 
} 
function printSelection() { 
if(isset($_POST['selectbox'])){ 
    return $_POST['selectbox']; 
} 
} 

$link = printSelection(); 
print($link); 

?> 

好像我已经用尽了所有的选项,但仍无法得到它的工作。宁愿不使用Globals。提前致谢!

回答

1

我认为问题是,你在文件末尾调用函数,之后你声明变量。您应该在文件开始时拨打printSelection(),然后将返回分配给$link。这应该解决它

+2

在'全球'的分配是不可能的。只是说。 –

+0

谢谢,但没有奏效。我现在编辑了这个问题以包含该文件中的所有代码。 –

+0

我认为'全球'将是唯一的选择。明天我会仔细看看代码 – JelleKerkstra

相关问题