我需要在函数之间使用PHP变量

时间:2014-03-06 09:01:32

标签: php html wordpress wordpress-plugin

需要WP插件的选项页面,用户可以通过从下拉列表中选择来删除链接。

创建选项页面,并在名为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 个答案:

答案 0 :(得分:1)

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