我正在为我的模板制作主题选项页面,并希望集成一个复选框。问题是,复选框不会保存该值。我的错误在哪里?嗯,我很无能......
<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_init(){
register_setting( 'wordpress_options', 'wordpress_theme_options');
}
function theme_options_add_page() {
add_theme_page('Options', 'Options', 'edit_theme_options', 'theme-options', 'wordpress_theme_options_page' );
}
function wordpress_theme_options_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false; ?>
[...]
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade">
<p><strong>Settings saved!</strong></p>
</div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'wordpress_options' ); ?>
<?php $options = get_option( 'wordpress_theme_options' ); ?>
<div id="admininterface">
[...]
<p>
<p class="before-form">Show RSS Icon:</p>
<?php $options = get_option( 'icon-rss-show' ); ?>
<input type="checkbox" name="$options[icon-rss-show]" value="1"<?php checked( 1 == $options['icon-rss-show'] ); ?> />
</p>
[...]
</div><!-- #admininterface -->
</form>
</div>
<?php } ?>
答案 0 :(得分:0)
添加另一个隐藏的 input
字段实际上会使表单在数据库中保存零值,如下所示:
<form>
<input type='hidden' value='0' name='selfdestruct'>
<input type='checkbox' value='1' name='selfdestruct'>
</form>
确保将隐藏的 input
放在您的常规输入上方,其值为 '0'
,并且与常规输入字段中的名称相同。
答案来自这里:https://stackoverflow.com/a/1992745/4688612
请将所有功劳归于该海报。