尽管使用文本,但不会保存自定义选项页面复选框

时间:2017-02-21 作者:Kristoffer M

这是我的代码:

add_action(\'admin_menu\', \'test_plugin_setup_menu\');                                                                                                     /**/
function test_plugin_setup_menu(){                                                                                                                      /**/
        add_submenu_page(\'options-general.php\',\'Forhandler options side\',\'Forhandler Options\', \'manage_options\', \'mine-første-options\', \'test_init\');   /**/
}                                                                                                                                                       /**/
function test_init(){                                                                                                                                   /**/
        //echo "<h1>Hello World!</h1>";
        ?>
        <div>
        <h1>Codehero Dealers</h1>
        <form action="options.php" method="post">
        <?php settings_fields(\'mine_plugin_options\'); ?>
        <?php do_settings_sections(\'mine-første-options\'); ?>

        <input name="Submit" type="submit" value="<?php esc_attr_e(\'Save Changes\'); ?>" />
        </form>
        </div>
        <?php
}
function mine_plugin_section_text() {
    echo \'<p>Her finder du indstillinger til forhandler delen.</p>\';
}
// add the admin settings and such
add_action(\'admin_init\', \'mine_plugin_admin_init\');
function mine_plugin_admin_init(){
register_setting( \'mine_plugin_options\', \'mine_plugin_options\', \'mine_plugin_options_validate\' );
add_settings_section(\'mine_plugin_main\', \'Main Settings\', \'mine_plugin_section_text\', \'mine-første-options\');
add_settings_field(\'mine_plugin_text_string\', \'Forhandler checkout indstilling\', \'mine_plugin_setting_string\', \'mine-første-options\', \'mine_plugin_main\');
}


function mine_plugin_setting_string() {

$options = get_option(\'mine_plugin_options\');
echo "<input id=\'plugin_checkbox\' name=\'mine_plugin_options[plugin_checkbox]\' type=\'checkbox\' value=\'true\' />";

}

// validate our options
function mine_plugin_options_validate($input) {
    /*
$options = get_option(\'mine_plugin_options\');
$options[\'text_string\'] = trim($input[\'text_string\']);
if(!preg_match(\'/^[a-z0-9]{}$/i\', $options[\'text_string\'])) {
$options[\'text_string\'] = \'\';
}
return $options; */
}
它表示设置已保存,但复选框恢复为未单击。

我对“创造你自己的选择”这件事还很陌生,所以任何帮助都将不胜感激。我按照本教程编写代码:http://ottopress.com/2009/wordpress-settings-api-tutorial/

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

不要注释掉验证代码请记住,您正在使用它来验证数据,因此它现在不返回任何内容,也不保存任何内容,请尝试以下操作:

add_action(\'admin_bar_menu\', \'make_parent_node\', 999);

function make_parent_node($wp_admin_bar) {
    $args = array(
        \'id\' => \'test1234\', // id of the existing child node (New > Post)
        \'title\' => \'test\', // alter the title of existing node
        \'parent\' => \'new-content\', // set parent to false to make it a top level (parent) node
        \'href\' => admin_url(\'admin.php?page=enter_timesheet\')
    );
    $wp_admin_bar->add_node($args);
}

add_action(\'admin_menu\', \'test_plugin_setup_menu\');

function test_plugin_setup_menu() { /**/
    add_submenu_page(\'options-general.php\', \'Forhandler options side\', \'Forhandler Options\', \'manage_options\', \'mine-første-options\', \'test_init\');
}

/**/

function test_init() { /**/
    //echo "<h1>Hello World!</h1>";
    ?>
    <div>
        <h1>Codehero Dealers</h1>
        <form action="options.php" method="post">
            <?php settings_fields(\'mine_plugin_options\'); ?>
            <?php do_settings_sections(\'mine-første-options\'); ?>

            <input name="Submit" type="submit" value="<?php esc_attr_e(\'Save Changes\'); ?>" />
        </form>
    </div>
    <?php
}

function mine_plugin_section_text() {
    echo \'<p>Her finder du indstillinger til forhandler delen.</p>\';
}

// add the admin settings and such
add_action(\'admin_init\', \'mine_plugin_admin_init\');

function mine_plugin_admin_init() {
    register_setting(\'mine_plugin_options\', \'mine_plugin_options\', \'mine_plugin_options_validate\');

    add_settings_section(\'mine_plugin_main\', \'Main Settings\', \'mine_plugin_section_text\', \'mine-første-options\');

    add_settings_field(\'mine_plugin_checkbox\', \'Forhandler checkout indstilling\', \'mine_plugin_setting_string\', \'mine-første-options\', \'mine_plugin_main\');

}

function mine_plugin_setting_string() {

    $options = get_option(\'mine_plugin_options\');

    echo "<input id=\'mine_plugin_checkbox\' name=\'mine_plugin_options[checkbox]\' type=\'checkbox\' value=\'1\'" . checked( 1, $options[\'checkbox\'], false ) . " />";
}

// validate our options
function mine_plugin_options_validate($input) {

    $newinput[\'checkbox\'] = trim($input[\'checkbox\']);
    return $newinput;
}
我们可以使用WordPress,而不是读取选项、设置条件并检查是否存在值checked 作用

SO网友:Phoenix Online

看来你错过了checked="checked" 输入值。。。

尝试以下操作:

$options = get_option(\'mine_plugin_options\');
$checked = ($options == \'true\' ? \' checked="checked"\' : \'\');
echo "<input id=\'plugin_checkbox\' name=\'mine_plugin_options[plugin_checkbox]\' type=\'checkbox\' value=\'true\' {$checked} />";