【问题标题】:wordpress backend checkbox won't save the value (checked)wordpress 后端复选框不会保存值(选中)
【发布时间】:2014-02-16 01:30:38
【问题描述】:

我正在为我的模板制作一个主题选项页面,并希望集成一个复选框。问题是,复选框不会保存值。我的错误在哪里?好吧,我一无所知...

<?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 } ?>

【问题讨论】:

  • 这是什么name="$options[icon-rss-show]"
  • 谢谢,我没有注意到这一点。我将其替换为 name="wordpress_theme_options[icon-rss-show]" 并删除了 。现在可以了。

标签: php wordpress variables checkbox wordpress-theming


【解决方案1】:

添加另一个隐藏的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

请将所有功劳寄给那张海报。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2018-06-17
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多