Settings API in Section

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

    }

}

Related posts

2 comments

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

  2. 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
    
        }
    
    }
    

Comments are closed.