对于我的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();
代码按预期工作。首次加载自定义页面时,复选框为“未选中”。如果我们选中/取消选中并保存,则可以保存。我看不出有什么问题。 (?) – brasofilo
恐怕你是对的。我只是把它安装在一个新的网站上,而且复选框确实是空的。也许还有一些保存的数据来自以前的试验。但至少它让我更了解整个事情,尽管我再次陷入困境。请期待我的下一个问题。越来越接近我的目标,并且乐于更多地了解WP插件。 – WendiT
你会在[wordpress.se]找到更多的插件开发者。标签[**''**](http://wordpress.stackexchange.com/questions/tagged/plugin-development?sort=frequent&pageSize=50)有一些相当不错的材料。 –
brasofilo