我还在学习设置API,所以请原谅我的noob问题。我有三个字段:复选框、颜色选择器和文本字段(它们正在工作),但我想这样做:安装主题后,将“选中”状态添加到我的复选框中,但允许用户“取消选中”,如果为空,则将默认颜色添加到我的颜色选择器中。。。我不知道怎么做,所以请帮帮我!
我的代码现在是这样的(谢谢@Chip Bennett!):
<?php
add_action(\'admin_menu\',\'create_theme_options_page\');
function create_theme_options_page(){
add_options_page(\'Theme Options\',\'Theme Options\',\'administrator\',__FILE__,\'build_options_page\');
}
function build_options_page(){
?>
<div id="theme-options-wrap">
<div style="float:left;margin-top:19px;position:absolute;" class="icon32" id="icon-tools"><br/></div>
<h2>My options page</h2>
<form method="post" action="options.php">
<?php settings_fields(\'plugin_options\');?>
<?php do_settings_sections(__FILE__); ?>
<p class="submit">
<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e(\'Save changes\');?>"/>
</p>
</form>
</div>
<?php
}
add_action(\'admin_init\',\'register_and_build_fields\');
function register_and_build_fields(){
register_setting(\'plugin_options\',\'plugin_options\',\'validate_setting\');
add_settings_section(\'main_section\',\'<br/><br/>Main Settings\',\'section_cb\',__FILE__);
add_settings_field(\'banner_heading\',\'Banner Heading:\',\'banner_heading_setting\',__FILE__,\'main_section\');
add_settings_field(\'display_sidebar\',\'Display Sidebar:\',\'display_sidebar_setting\',__FILE__,\'main_section\');
add_settings_field(\'display_colorpicker\',\'Display Colorpicker:\',\'display_colorpicker_setting\',__FILE__,\'main_section\');
}
//Install Iris colorpicker
add_action( \'admin_enqueue_scripts\', \'mw_enqueue_color_picker\' );
function mw_enqueue_color_picker( $hook_suffix ) {
// first check that $hook_suffix is appropriate for your admin page
wp_enqueue_style( \'wp-color-picker\' );
wp_enqueue_script( \'my-script-handle\', get_template_directory_uri() . \'/js/my-script.js\', array( \'wp-color-picker\' ), false, true );
}
//Colorpicker
//Now how to add a default color on theme install?
function display_colorpicker_setting(){
$options = get_option(\'plugin_options\');
echo"<input type=\'text\' id=\'color1\' name=\'plugin_options[display_colorpicker]\' value=\'{$options[\'display_colorpicker\']}\'/>";
}
//Sidebar checkbox
//Now how to add a default state "checked" on theme install?
function display_sidebar_setting(){
$options = get_option(\'plugin_options\');
echo "<input type=\'checkbox\' name=\'plugin_options[display_sidebar]\' {$options[\'display_sidebar\']} id=\'display_sidebar\' />";
}
//Banner Heading
function banner_heading_setting(){
$options = get_option(\'plugin_options\');
echo "<input name=\'plugin_options[banner_heading]\' type=\'text\' value=\'{$options[\'banner_heading\']}\'/>";
}
//Validate our settings here
function validate_setting($plugin_options){
return $plugin_options;
}
function section_cb(){
}
?>
最合适的回答,由SO网友:s_ha_dum 整理而成
可以将默认值传递给get_option
. 第二个参数是回退。
$default
(混合)(可选)如果未返回值,则返回的默认值(即,该选项不在数据库中)。
Default: false
因此,例如,代替:
$options = get_option(\'plugin_options\');
您需要:
$options = get_option(\'plugin_options\',array(\'display_sidebar\' => true));
也可以使用插入默认选项
add_option
, 这是“一种将命名选项/值对添加到选项数据库表的安全方法。如果该选项已存在,则不会执行任何操作。”
我会做后者。这应该更容易。