2013-08-04 83 views
1

我有一个复选框,其默认状态为未选中:WordPress的设置页面,如何默认复选框的状态来检查?

<?php 
function edit_theme_settings() { 
    if (get_option('sold_text') == true) { $display = 'checked'; } 
    else { $display = ''; } 
    update_option('sold_text', $display); 
?> 

<input type="checkbox" name="sold_text" id="sold_text" <?php echo get_option('sold_text'); ?> /> 

我想它的默认状态为选中第一时间显示的形式,随后其“选中”状态应该由定义get_option( 'sold_text')。

+0

和功能'get_option()'? – 2013-08-04 21:18:48

+0

get_option()呢? – Andy

回答

3

既不建议的工作对我来说在这种情况下,但我想我已经通过使用add_option()

自己解决它一个安全的方式,将一个命名的选项/值对添加到选项数据库表。 如果该选项已存在,则不执行任何操作

所以我做了:add_option('sold_text')的值'checked',因此该复选框默认为选中状态。现在,由于选项已经存在add_option()什么也不做下一次加载窗体或提交和update_option()处理的复选框状态更新中...

<?php 
function edit_theme_settings() { 

add_option('sold_text', 'checked'); 

if (get_option('sold_text') == true) { $display = 'checked'; } 
else { $display = ''; } 
update_option('sold_text', $display); 
?> 

<input type="checkbox" name="sold_text" id="sold_text" <?php echo get_option('sold_text'); ?> /> 
1

复选框被存储为0或1(为未选中,选中),所以你需要的东西是这样的:

<input type="checkbox" name='sold_text' id='sold_text' value="1" <?= checked(get_option('sold_text'), 1, false);?> /> 

WP的检查()函数是专为这样的:http://codex.wordpress.org/Function_Reference/checked

相关问题