在所附的屏幕截图中,您可以看到我们正在使用的主题选项的结构。共有8个选项卡,对于每个选项卡,我们使用类似于下面所示的代码创建了自定义字段。这就是我们为每个字段设置默认值的方式。(即默认=)http://facebook.com/samplefacebooktitle\')
function facebook_callback() {
$item =\'facebook\';
$default =\'http://facebook.com/samplefacebooktitle\';
$group = \'general_display_options\';
$input = create_input($item,$default,$group);
echo $input;
}
然后是create\\u输入函数:
function create_input($item,$default,$group) {
$obj = $group.\'[\'.$item.\']\';
$options = get_option($group);
if ($options[$item] != \'\') {
$value= $options[$item];
} else {
$value = $default;
}
return \'<input type="text" id="\'.$item.\'" name="\'.$obj.\'" value="\'. $value .\'" />\';
}
但是,为了初始化默认值,我们需要在第一次加载主题时单独保存每个选项卡,并且该主题将由其他国家的许多公司为国际化定制。所以,问题是,是否有一种简单的方法可以预加载这些默认值,并在第一次保存主题选项之前自动保存它们。如果没有,当您单击任何给定选项卡上的“保存”时,是否有人知道如何一次保存所有主题选项?
-------编辑---------------------
感谢Chip Bennett帮助我完成这项工作!下面的代码有点粗糙,但它可以工作,并设置了主题激活的默认值。还添加了一个检查,以查看值是否为“”,如果是,我们将其重新设置为在create\\u general\\u defaults()函数中创建的默认值。请随时告诉我优化此代码的方法。
<?php
function theme_menu() {
add_theme_page(
\'My Theme\',
\'My Theme\',
\'administrator\',
\'theme_options\',
\'theme_display\'
);
}
add_action(\'admin_menu\', \'theme_menu\');
function theme_display( $active_tab = \'\') {
?>
<div class="wrap">
<!-- add icon to the page -->
<div id="icon-themes" class="icon32"></div>
<h2>Theme Options</h2>
<?php settings_errors(); ?>
<?php
if(isset($_GET[\'tab\'])) {
$active_tab = $_GET[\'tab\'];
} else if ($active_tab == \'nav_options\') {
$active_tab = \'nav_options\';
} else {
$active_tab = \'display_options\';
}
?>
<h2 class="nav-tab-wrapper">
<a href="?page=theme_options&tab=display_options" class="nav-tab <?php echo $active_tab == \'display_options\' ? \'nav-tab-active\' : \'\'; ?>">General</a>
<a href="?page=theme_options&tab=nav_options" class="nav-tab <?php echo $active_tab == \'nav_options\' ? \'nav-tab-active\' : \'\'; ?>">Nav</a>
</h2>
<form method="post" action="options.php">
<?php
if( $active_tab == \'display_options\' ) {
settings_fields( \'theme_display_options\' );
do_settings_sections( \'theme_display_options\' );
} else if( $active_tab == \'nav_options\' ) {
settings_fields( \'theme_nav_options\' );
do_settings_sections( \'theme_nav_options\' );
}
submit_button();
?>
</form>
</div>
<?php
}
/* -------------------- *
* register settings
* -------------------- */
//global $theme_display_get_options;
// $theme_display_get_options = theme_display_get_options();
function initialize_theme_options() {
// verify if the options exist
if(false == get_option(\'theme_display_options\')) {
add_option(\'theme_display_options\');
}
add_settings_section(
\'display_settings_section\',\'General Options\',\'display_options_callback\',\'theme_display_options\'
);
add_settings_field(\'facebook_alt\',\'Facebook Alt Tag\',\'facebook_alt_callback\',\'theme_display_options\',\'display_settings_section\');
register_setting(\'theme_display_options\',\'theme_display_options\');
}
add_action(\'admin_init\',\'initialize_theme_options\');
function display_options_callback() {
echo \'<p>Enter the necessary values for the following fields.</p>\';
}
function create_general_defaults() {
global $general_defaults;
$general_defaults = array(
\'theme_title\' => \'My Title Yo\',
\'facebook\' => \'http://facebook.com\',
\'facebook_alt\' => \'My default\'
);
//save these in case something is blank and it will return to its default value
$general_orig_defaults = apply_filters(\'my_options_defaults\', $general_defaults);
return $general_orig_defaults;
}
create_general_defaults();
function get_general_defaults() {
$option_defaults = create_general_defaults();
global $the_theme_options;
$the_theme_options = wp_parse_args( get_option(\'theme_display_options\', array() ), $option_defaults );
return $the_theme_options;
}
get_general_defaults();
function facebook_alt_callback() {
global $general_defaults;
global $orig_defaults;
$orig_defaults = $general_defaults;
$item =\'facebook_alt\';
$group = \'theme_display_options\';
$form_object = \'text\';
callback_standard($item, $group, $form_object, $orig_defaults);
}
function callback_standard($item, $group, $form_object) {
global $the_theme_options;
global $orig_defaults; // defaults set in the
$default = $the_theme_options[$item];
if($default == \'\') {
$default = $orig_defaults[$item];
}
$obj = $group.\'[\'.$item.\']\';
if ($form_object == \'text\') {
echo \'<input type="text" id="\'.$item.\'" name="\'.$obj.\'" value="\'. $default .\'" style="width:300px;" />\';
} else if ($form_object == \'textarea\') {
\'<textarea id="\'.$item.\'" name="\'.$obj.\'" rows="5" style="width:500px" />\'. $value .\'</textarea>\';
}
}
/* --------------------------------------------------------
* Navigation Options
* --------------------------------------------------------*
*/
// left off for abbreviated example
/* ----------------------------------------------------
* Templated settings field output
* ----------------------------------------------------
*/
function create_input($item,$default,$group) {
$obj = $group.\'[\'.$item.\']\';
$options = get_option($group);
if ($options[$item] != \'\') {
$value= $options[$item];
} else {
$value = $default;
}
return \'<input type="text" id="\'.$item.\'" name="\'.$obj.\'" value="\'. $value .\'" style="width:300px;" />\';
}
function create_textarea($item,$default,$group) {
$obj = $group.\'[\'.$item.\']\';
$options = get_option($group);
if ($options[$item] != \'\') {
$value = $options[$item];
} else {
$value = $default;
}
$value = stripslashes($value);
return \'<textarea id="\'.$item.\'" name="\'.$obj.\'" rows="5" style="width:500px" />\'. $value .\'</textarea>\';
}
?>