0

I'm trying to create a really simple options page for my wordpress site that just contains one option - a conversion rate for a currency. I have the following code:

add_action('admin_init', 'currency_options_set');
add_action( 'admin_menu', 'admin_menu' );

function admin_menu () {
    add_options_page( 'Currency Options','Currency Options','manage_options','options_currency', 'settings_page' );
}

function currency_options_set(){
    register_setting( 'currency_options_group', 'currency_options' );
}

function  settings_page () {

    $default_options = array( 'currency_eur' => "1.1" );
    $options = get_option('currency_options', $default_options);
    ?>
    <form method="post" action="options.php">
    <?php

        settings_fields("currency_options_group"); ?>

        <table class="form-table">
            <tr>
                <th scope="row">EUR:</th>
                <td>
                    <input type="text" name="currency_options_group[currency_eur]" size="40" value="<?php echo stripslashes($options["currency_eur"]); ?>" />
                </td>
            </tr>
        </table>

        <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
        </p>
    </form>
<?php
}

The new options page shows and I can see the form but when I submit it, no options are saved to the wp_options table. What am I missing here?

0

Just tested your code, found an error in the input name. Change currency_options_group to currency_options will make it work. The modified code:

add_action('admin_init', 'currency_options_set');
add_action( 'admin_menu', 'admin_menu' );

function admin_menu () {
    add_options_page( 'Currency Options','Currency Options','manage_options','options_currency', 'settings_page' );
}

function currency_options_set(){
    register_setting( 'currency_options_group', 'currency_options' );
}

function  settings_page () {

    $default_options = array( 'currency_eur' => "1.1" );
    $options = get_option('currency_options', $default_options);
    ?>
    <form method="post" action="options.php">
    <?php

        settings_fields("currency_options_group"); ?>

        <table class="form-table">
            <tr>
                <th scope="row">EUR:</th>
                <td>
                    <input type="text" name="currency_options[currency_eur]" size="40" value="<?php echo stripslashes($options["currency_eur"]); ?>" />
                </td>
            </tr>
        </table>

        <p class="submit">
            <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
        </p>
    </form>
<?php
}
0

You have a little mixup in the <input> field of your options page.

In your case, the name of the input should be the name of the option you want to save, not an array of the options_group, as well as the output should be just the option, not an $array of the options_group.

The options would be like this:

$default_options = '1.1';
$options = get_option('currency_options', $default_options);

Your correct <input> would be this:

<input type="text" name="currency_options" size="40" value="<?php echo stripslashes($options); ?>" />

Wordpress automatically knows that currency_options is part of the currency_options_group (through register_setting), and you do not need to use it here.

The options_group is used in the Settings API for letting you automatically generate wp-admin settings pages by registering your settings and using a few callbacks to control the output.

One simple setting is easier and faster to do in your way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.