GET_OPTION不起作用的复选框

时间:2018-11-19 作者:Alt C

我试图弄清楚如何在选项页面中实现复选框。我可以获取默认值,但不能保存和检索它。我正在使用get_option()checked() 功能

 <form method="post" action="options.php" id="theme-options-form">
            <?php settings_fields(\'wppl-settings-group-new\');?>
            <?php do_settings_sections(\'wppl-settings-group-new\');?>

    <?php 

     $default_values = array(
    0 => array(
        \'id\'             => \'0\',
        \'checkbox_check\' => \'1\',     
    ),
    1 => array(
        \'id\'             => \'1\',
        \'checkbox_check\' => \'0\',       
    ));

     $checkboxoutput = get_option(\'saved_value\', $default_values );

    foreach ($checkboxoutput as $value_new) {

         if (isset($value_new[\'checkbox_check\'])) {
            $checkbox_check = $value_new[\'checkbox_check\'];                   
            } 
         else{ 
            $checkbox_check = $checkboxoutput[$id][\'checkbox_check\']; //if not saved use the datefrom defualt ->ID passed
            }

         ?>
         <li class="sortable-item flexit">

            <input type="checkbox" class="form-check-input" id="exampleCheck1" name="checkboxoutput[<?php echo (int)$id; ?>][checkbox_check]" value="1"<?php checked(\'1\' , $checkbox_check);  ?> 
            />
            <input type="hidden" name="checkboxoutput[<?php echo (int)$id; ?>][checkbox_check]"   value="<?php echo (int)$checkbox_check; ?>" />

         </li>
    <?php submit_button();?>
    </form>
它正在获取默认值,但没有保存?请帮忙

1 个回复
最合适的回答,由SO网友:Anton Lukin 整理而成

使用复选框选项添加设置页面的完整代码。您不需要循环和其他东西。WordPress会为你做一切。在中获取更多信息Codex

function wpse_319648_render_popup() {
    $options = get_option(\'wpse_319648_checkbox\');
    $default = isset($options[\'popup\']) ? $options[\'popup\'] : 0;

    printf(
        \'<input type="checkbox" name="%1$s[popup]" value="1" %2$s>\',
        \'wpse_319648_checkbox\',
        checked($default, 1, false)
    );
}

function wpse_319648_settings_page() {
    echo \'<form class="wrap" action="options.php" method="post">\';
    settings_fields(\'wpse-319648-settings\');
    do_settings_sections(\'wpse-319648-settings\');
    submit_button();
    echo \'</form>\';
}

add_action(\'admin_init\', function() {
    register_setting(\'wpse-319648-settings\', \'wpse_319648_checkbox\');

     add_settings_section(
        \'wpse-319648-section\',
        __(\'Settings\', \'theme\'),
        [],
        \'wpse-319648-settings\'
    );

    add_settings_field(
        \'popup\',
        __(\'Show popup\', \'theme\'),
        \'wpse_319648_render_popup\',
        \'wpse-319648-settings\',
        \'wpse-319648-section\'
    );
});

add_action(\'admin_menu\', function() {
    add_submenu_page(\'options-general.php\',
        __(\'Options\', \'theme\'),
        __(\'Options\', \'theme\'),
        \'manage_options\',
        \'my-page\',
        \'wpse_319648_settings_page\'
    );
});
希望有帮助。

结束

相关推荐

是否以编程方式取消序列化WP_OPTIONS选项?

我需要以编程方式取消wp\\U选项的序列化。我正在创建一个Java应用程序来更改数据库信息,例如wp\\u选项上的一些选项,这样我就不需要访问面板来进行更改。如何取消序列化选项,如 a:4:{i:0;s:5:\"posts\";i:1;s:5:\"pages\";i:2;s:4:\"tags\";i:3;s:10:\"categories\";} 或 a:1:{s:12:\"_multiwidget\";i:1;} 更改它们并重新序列化到数据库?谢谢