1

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

    }

}
3
  • have you checked options table in db?
    – iEmanuele
    Jul 26, 2013 at 13:53
  • sorry i see now your reply on other answer
    – iEmanuele
    Jul 26, 2013 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, 2013 at 14:06

2 Answers 2

1

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

1
  • thanks so much, never realized that the register_setting actually have to go with 'general'. Silly me
    – Angela
    Jul 26, 2013 at 15:02
1

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

    }

}
2
  • 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, 2013 at 13:13
  • As Pontus pointed out main issue is with first argument of register_settings which should be 'general'
    – M-R
    Jul 26, 2013 at 14:54

Your Answer

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

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