0

Need an options page for WP plugin, where the user can remove a link by choosing from a dropdown.

The options page is created and the variable is set in a function called printSelection(). This function returns the value in the dropdown box from the WP dashboard.

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

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

The $link variable is being printed successfully to the WP options page. What I would like to do is use this variable in the frontend as well, just after the form. The full code is here:

<?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);

?>

It seems like I have exhausted every option but still can't get it to work. Would prefer not to use Globals either. Thanks in advance!

1 Answer 1

1

I think the problem is, that you call the function at the end of the file and after that you declare the variable. You should call printSelection() at the beginning of the file and after that assign the return to $link. That should solve it

Sign up to request clarification or add additional context in comments.

4 Comments

Assignments in global are not possible. Just saying.
Thanks but that didn't work. I've edited the question now to include all of the code from that file.
I think global will be the only option. I'll take a closer look at the code tomorrow
Thanks. I still haven't got it to work so an answer using globals would be great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.