Settings API in Section

时间:2013-07-26 作者:Angela

我有一个代码片段,它使用设置API在设置->常规中创建了一个选项。但是,这些值没有更新,我想知道是否有人可以帮助指出以下代码的问题

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

    }

}

2 个回复
最合适的回答,由SO网友:Pontus Abrahamsson 整理而成

给你,我的朋友:

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;
}
将该值用于:get_option( \'msp_duration\' );

SO网友:M-R

而不是checked, 您应该使用selected 作用

因此,您的msp\\u settings\\u回调将如下所示

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

    }

}

结束

相关推荐