Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wrote a simple test code for testing saving data using Setting API, but it doesn't seem to work. it realy driving me crazy. Please help me point out the problem, thanks!

function test_add_options_page() {
    add_menu_page(
        'Test Save Option',
        'Test Save',
        'manage_options',
        'testpage',
        'test_save_page'
    );
}
add_action('admin_menu', 'test_add_options_page');

function test_save_page() {
    ?>
        <form action="options.php" method="post">
            <?php 
                settings_errors();
                settings_fields('testpage');
                do_settings_sections('testpage');
                submit_button();
            ?>
        </form>

    <?php   
}

function test_add_settings() {
    add_settings_section(
        'section_1',
        'Section 1',
        'output_section_desc',
        'testpage'
    );

    add_settings_field(
        'field_id',
        'Field Title',
        'add_field_callback',
        'testpage',
        'section_1'
    );

    register_setting('testpage', 'testpage');
}
add_action('admin_init', 'test_add_settings');

function output_section_desc() {
    echo 'Section Description...';
}

function add_field_callback() {
    echo '<input type="test" id="field_id" />';
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

First off, your input field is missing a name attribute.
The id attribute is a unique identifier, mainly used by the visitor's browser, to target elements via CSS or JS.
The name attribute is an identifier for form elements to be used in the POST or GET request on form submission.

Also, test is not a valid input type.

So far, that's generic and not at all specific to the Settings API.

Your callback should hence look like this:

function add_field_callback() {
    echo '<input type="text" id="field_id" name="testpage[\'field_id\']"/>';
}

The Settings API saves options in option groups. You defined your group to be called 'testpage'. Hence to retrieve the saved value, you'd do something along the lines of:

$testpage_values = get_option( 'testpage' );
$field_value = isset( $testpage_values['field_id'] ) ? $testpage_values['field_id'] : 'Not set.';
echo $field_value;
share|improve this answer
    
Got it. thanks Pille, for the quick answer, it's clear and detailed. –  user34894 Jul 4 '13 at 9:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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