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 have a snippet that creates an option in Settings->General using Settings API. However, the values don't get updated and I was wondering if anyone can help point out the problem with the following code

function admin_init_callback() {

    register_setting( 'msp_settings', 'msp_duration' );

    add_settings_section( 'msp_settings', __('Multisite Posts Configuration'), false, 'general' );

    add_settings_field( 'msp_duration', __('Cache Duration'), array($this, 'msp_settings_callback'), 'general', 'msp_settings', array(
        "id"    => "msp_duration",
        "type"  => "dropdown",
        "values"=> array(
            array(
                "name"  => __('Every 6 hours'),
                "value" => 60 * 60 * 6,
            ),
            array(
                "name"  => __('Every 12 hours'),
                "value" => 60 * 60 * 12,
            ),
            array(
                "name"  => __('Every 24 hours'),
                "value" => 60 * 60 * 24,
            ),
            array(
                "name"  => __('Every 48 hours'),
                "value" => 60 * 60 * 48,
            ),
            array(
                "name"  => __('Every Week'),
                "value" => 60 * 60 * 24 * 7,
            ),
        )
    ) );

    return;

}

function msp_settings_callback($args) {

    $value = get_option( $args["id"] );

    if("dropdown" == $args["type"]) {

        var_dump($value);
        ?>
        <select name="<?php echo $args["id"]; ?>">
            <?php
                foreach ($args["values"] as $option) {
                    echo '<option value="' . $option["value"] . '" ';
                    checked( $value, $option["value"], $echo = true );
                    echo '>' . $option["name"] . '</option>';
                }
            ?>
        </select>
        <?php

    }

}
share|improve this question
    
have you checked options table in db? –  iEmanuele Jul 26 '13 at 13:53
    
sorry i see now your reply on other answer –  iEmanuele Jul 26 '13 at 13:58
    
did you see your value sent to the server? Firebug it in "Net" tab, search for POST options.php –  iEmanuele Jul 26 '13 at 14:06

2 Answers 2

up vote 1 down vote accepted

Here you go my friend:

add_filter( 'admin_init' , 'register_fields' );

function register_fields() 
{
    register_setting( 'general', 'msp_duration', 'esc_attr' );
    add_settings_field( 'msp_duration', __('Cache Duration') ,'fields_html', 'general' );
}

function fields_html() 
{
    $value = get_option( 'msp_duration' );
    $fields = array(
        array(
            "name"  => __('Every 6 hours'),
            "value" => 60 * 60 * 6,
        ),
        array(
            "name"  => __('Every 12 hours'),
            "value" => 60 * 60 * 12,
        ),
        array(
            "name"  => __('Every 24 hours'),
            "value" => 60 * 60 * 24,
        ),
        array(
            "name"  => __('Every 48 hours'),
            "value" => 60 * 60 * 48,
        ),
        array(
            "name"  => __('Every Week'),
            "value" => 60 * 60 * 24 * 7,
        ),
    );

    $output = '<select name="msp_duration">';

        foreach( $fields as $option ) 
        {
            $output .= '<option value="'. $option['value'] .'" '. selected( $option['value'], $value, false ) .'>';
                $output .= $option["name"];
            $output .= '</option>';
        }

    $output .= '</select>';

    echo $output;
}

Use the value with: get_option( 'msp_duration' );

share|improve this answer
    
thanks so much, never realized that the register_setting actually have to go with 'general'. Silly me –  Angela Jul 26 '13 at 15:02

Instead of checked, you should use selected function

So your msp_settings_callback will look like this

function msp_settings_callback($args) {

    $value = get_option( $args["id"] );

    if("dropdown" == $args["type"]) {

        var_dump($value);
        ?>
        <select name="<?php echo $args["id"]; ?>">
            <?php
                foreach ($args["values"] as $option) {
                    echo '<option value="' . $option["value"] . '" ';
                    selected( $value, $option["value"]);
                    echo '>' . $option["name"] . '</option>';
                }
            ?>
        </select>
        <?php

    }

}
share|improve this answer
    
My problem is that I don't see any value saved into the DB. I can change the selected np, but this doesn't solve what I have at hand. –  Angela Jul 26 '13 at 13:13
    
As Pontus pointed out main issue is with first argument of register_settings which should be 'general' –  M-R Jul 26 '13 at 14:54

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.