【问题标题】:Checkbox default unchecked复选框默认未选中
【发布时间】:2013-10-18 11:01:57
【问题描述】:

对于我的 Wordpress 插件选项页面,我想使用复选框来设置选项。我正在使用此代码将其添加到选项页面,并且效果很好。除了我希望复选框默认设置为未选中。我该怎么做?

public function id_number_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s" />',
'add_image_dimesions',
checked( isset( $this->options['add_image_dimesions'] ), true, false )
);

编辑:我要使用的完整代码是这样的

class MySettingsPage
{
/**
 * Holds the values to be used in the fields callbacks 
 */
private $options;

/**
 * Start up
 */
public function __construct()
{
    add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
    add_action( 'admin_init', array( $this, 'page_init' ) );
}

/**
 * Add options page
 */
public function add_plugin_page()
{
    // This page will be under "Settings"
    add_options_page(
        'Settings Admin', 
        'Custom functions', 
        'manage_options', 
        'cus-func-admin', 
        array( $this, 'create_admin_page' )
    );
}

/**
 * Options page callback
 */
public function create_admin_page()
{
    // Set class property
    $this->options = get_option( 'cus_func_option' );
?>
<div class="wrap">
    <?php screen_icon(); ?>
    <h2>Custom Functions</h2>           
    <form method="post" action="options.php">
    <?php
        // This prints out all hidden setting fields
        settings_fields( 'cus_func_group' );   
        do_settings_sections( 'cus-func-admin' );
        submit_button(); 
    ?>
    </form>
</div>
<?php
}

/**
 * Register and add settings
 */
public function page_init()
{        
    register_setting(
        'cus_func_group', // Option group
        'cus_func_option', // Option name
        array( $this, 'sanitize' ) // Sanitize
    );

    add_settings_section(
        'setting_section_id', // ID
        'Which Custom Functions do you want to use?', // Title
        array( $this, 'print_section_info' ), // Callback
        'cus-func-admin' // Page
    );  

    add_settings_field(
        'add_image_dimesions', // ID
        'Add image dimesions to the Media Page', // Title 
        array( $this, 'id_number_callback' ), // Callback
        'cus-func-admin', // Page
        'setting_section_id' // Section           
    );      

    add_settings_field(
        'title', 
        'Title', 
        array( $this, 'title_callback' ), 
        'cus-func-admin', 
        'setting_section_id'
    );      
}

/**
 * Sanitize each setting field as needed
 *
 * @param array $input Contains all settings fields as array keys
 */
public function sanitize( $input )
{
    $new_input = array();
    if( isset( $input['add_image_dimesions'] ) )
        $new_input['add_image_dimesions'] = absint( $input['add_image_dimesions'] );

    if( isset( $input['title'] ) )
        $new_input['title'] = sanitize_text_field( $input['title'] );

    return $new_input;
}

/** 
 * Print the Section text
 */
public function print_section_info()
{
    print 'Pick your Custom Functions below:';
}

/** 
 * Get the settings option array and print one of its values
 */
public function id_number_callback()
{


    printf(
        '<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s" />',
'add_image_dimesions',
checked( isset( $this->options['add_image_dimesions'] ), true, false )
);

}

/** 
 * Get the settings option array and print one of its values
 */
public function title_callback()
{


printf(
        '<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" %2$s />',
'title',
checked( isset( $this->options['title'] ), true, false )
);  


}
}

if( is_admin() )
$my_settings_page = new MySettingsPage();

【问题讨论】:

  • 代码按预期工作。首次加载自定义页面时,复选框为unchecked。如果我们选中/取消选中并保存,则保存正常。我不明白问题是什么。 (?)
  • 恐怕你是对的。我刚刚将它安装在一个新网站上,复选框确实是空的。也许还有一些先前试验的保存数据。但至少它让我更好地理解了整个事情,尽管我再次陷入困境。请期待我的下一个问题。离我的目标越来越近,很高兴了解更多关于 WP 插件的信息。
  • 您可以在WordPress Development 找到更多插件开发人员。标签&lt;plugin-development&gt; 有一些不错的素材。
  • 感谢您的提示,brasofilo。我有时会通过 Google 搜索到达那里,但也许我也应该看看插件开发中的内容。

标签: php wordpress checkbox


【解决方案1】:

参考WordPress Documentation for checked

您的代码中isset( $this-&gt;options['add_image_dimesions'] ) 的值是多少?

这是决定它是否被检查的原因。

【讨论】:

  • 是的,我试过这个,但是当我想选中复选框并保存它时,该框又被取消选中了。
  • $this->options['add_image_dimensions'] 的值是多少?
  • 代码中没有你的 $options 变量集,所以这应该总是返回false。您需要设置/保存选项值才能使其起作用。
  • 不知道我是否理解。该代码基于stackoverflow.com/questions/17798378/…,而不是我想要复选框的文本字段。哪种有效,只需要将默认改为unchecked即可。
  • 您是否希望 default 未选中,但如果更改并保存,则显示已选中?如果是这样,那么您需要在您的课程中正确设置 $option 值。现在,$this->option 永远不会设置/更新。
【解决方案2】:

如果您希望在页面加载时未选中复选框始终,那么您只需删除第二个 printf 令牌 %2$2 和 printf 的第二个参数 checked( isset( $this-&gt;options['add_image_dimesions'] ), true, false ),这会给你这个:

public function id_number_callback()
{
printf(
'<input id="%1$s" name="cus_func_option[%1$s]" type="checkbox" />',
'add_image_dimesions'
);

【讨论】:

  • 与cale_b相同,当我勾选并保存它时,该框再次恢复为未选中状态(而我现在使用的代码在保存后留下勾号,并在之后删除勾号我保存未选中的框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-24
  • 2018-01-15
  • 2018-12-31
  • 2017-03-27
  • 2015-10-05
相关资源
最近更新 更多