【发布时间】: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 找到更多插件开发人员。标签
<plugin-development>有一些不错的素材。 -
感谢您的提示,brasofilo。我有时会通过 Google 搜索到达那里,但也许我也应该看看插件开发中的内容。