我试图通过教程在管理设置页面中显示选项卡式设置,但对我来说,我收到了两次保存设置的通知。
我认为这是由于不同的设置字段,但基于选项卡的选择,我只显示一个使用php条件的表单。所以可能只有一种形式settings_fields()
但在我保存时,它是如何显示两次保存的设置的。我什么都不知道。有人能帮忙吗。
Code
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>Sandbox Theme Options</h2>
<?php settings_errors(); ?>
<?php $active_tab = isset( $_GET[ \'tab\' ] ) ? $_GET[ \'tab\' ] : \'d_options\'; ?>
<h2 class="nav-tab-wrapper">
<a href="?page=followshape&tab=d_options" class="nav-tab <?php echo $active_tab == \'d_options\' ? \'nav-tab-active\' : \'\'; ?>">Display</a>
<a href="?page=followshape&tab=s_options" class="nav-tab <?php echo $active_tab == \'s_options\' ? \'nav-tab-active\' : \'\'; ?>">Shape</a>
<a href="?page=followshape&tab=Stat" class="nav-tab <?php echo $active_tab == \'Stat\' ? \'nav-tab-active\' : \'\'; ?>">Stat</a>
</h2>
<?php if( $active_tab == \'d_options\' ) { ?>
<form method="post" action="options.php">
<?php
settings_fields( \'shapet\' );
?>
<div class="show_type">
<input type="radio" name="shape" id="radio1" value="1"<?php checked(1,get_option(\'shape\')); ?>/>
<label for="radio1"><img src="<?php echo plugins_url(); ?>/slider/images/1.jpg"></label>
<input type="radio" id="radio2" name="shape" value="2"<?php checked(2,get_option(\'shape\')); ?> />
<label for="radio2"><img src="<?php echo plugins_url(); ?>/slider/images/2.jpg"></label>
<input type="radio" id="radio3" name="shape" value="3"<?php checked(3,get_option(\'shape\')); ?> />
<label for="radio3"><img src="<?php echo plugins_url(); ?>/slider/images/3.jpg"></label>
<input type="radio" id="radio4" name="shape" value="4"<?php checked(4,get_option(\'shape\')); ?> />
<label for="radio4"><img src="<?php echo plugins_url(); ?>/slider/images/4.jpg"></label>
<input type="radio" id="radio5" name="shape" value="5"<?php checked(5,get_option(\'shape\')); ?> />
<label for="radio5"><img src="<?php echo plugins_url(); ?>/slider/images/5.jpg"></label>
<input type="text" name="sh_color" value="<?php echo get_option(\'sh_color\'); ?>" class="ir" />
</div>
<input type="submit" name="submit" />
</form>
<?php
} if($active_tab == \'s_options\') { ?>
<form method="post" action="options.php">
<?php
settings_fields( \'social\' );
for($i=1;$i<=8;$i++){
?>
<lable>Social Network<?php echo $i; ?> </lable><input type="text" name="sfsocial_net<?php echo $i; ?>" value="<?php echo get_option(\'sfsocial_net\'.$i); ?>"/>
<lable>Show </lable><input type="checkbox" name="sfsocial_net_show<?php echo $i; ?>" value="1"<?php checked(1,get_option(\'sfsocial_net_show\'.$i)); ?> />
<lable>Lable</lable><input type="text" name="sfsocial_lable<?php echo $i; ?>" value="<?php echo get_option(\'sfsocial_lable\'.$i); ?>"/>
<br>
<?php }
?>
<input type="submit" name="submit" />
</form>
<?php } if ($active_tab == \'Stat\') { ?>
<form method="post" action="options.php">
<?php
echo "graph";
settings_fields(\'stat\');?>
<lable>facebook</lable><input type="text" name="facebookun" value="<?php echo get_option(\'facebookun\'); ?>"/>
<input type="submit" name="submit" />
</form>
<?php
}?>
</div>
Tried After posting Question from @MrJustin advice 在我知道我应该使用add_settings_section
和add_settings_field
除了register_setting
下面是我在另一个教程中尝试的代码。
function registering_settings(){
register_setting( \'my-settings-group\', \'my-setting\' );
add_settings_section( \'section-one\', \'Section One\', \'section_one_callback\', \'my-plugin\' );
add_settings_field( \'field-one\', \'Field One\', \'field_one_callback\', \'my-plugin\', \'section-one\' );
}
add_action(\'admin_init\',\'registering_settings\');
function callback_testing(){
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>Sandbox Theme Options</h2>
<?php settings_errors(); ?>
<?php $active_tab = isset( $_GET[ \'tab\' ] ) ? $_GET[ \'tab\' ] : \'d_options\'; ?>
<h2 class="nav-tab-wrapper">
<a href="?page=slug_testing&tab=d_options" class="nav-tab <?php echo $active_tab == \'d_options\' ? \'nav-tab-active\' : \'\'; ?>">Display Options</a>
<a href="?page=slug_testing&tab=s_options" class="nav-tab <?php echo $active_tab == \'s_options\' ? \'nav-tab-active\' : \'\'; ?>">Social Options</a>
<a href="?page=slug_testing&tab=S" class="nav-tab <?php echo $active_tab == \'S\' ? \'nav-tab-active\' : \'\'; ?>">Statistics</a>
<a href="?page=slug_testing&tab=new" class="nav-tab <?php echo $active_tab == \'new\' ? \'nav-tab-active\' : \'\'; ?>">New</a>
</h2>
<?php if ($active_tab == \'new\') { ?>
<form action="options.php" method="POST">
<?php settings_fields( \'my-settings-group\' ); ?>
<?php do_settings_sections( \'my-plugin\' ); ?>
<?php submit_button(); ?>
</form>
<?php } ?>
<?php if ($active_tab == \'d_options\') { ?>
<?php } ?>
<?php if ($active_tab == \'s_options\') { ?>
<?php } ?>
</div><!-- /.wrap -->
<?php
}
function section_one_callback() {
echo \'Some help text goes here.\';
}
function field_one_callback() {
$setting = esc_attr( get_option( \'my-setting\' ) );
echo "<input type=\'text\' name=\'my-setting\' value=\'$setting\' />";
}
即使尝试了这个,我还是保存了两次设置。但只有一种形式
section
有一个
settings_fields
总共只有一种形式。为什么当我点击保存更改时,它会显示两次保存的设置。
Edit
register_setting( \'my-plugin\', \'my-setting\' );
add_settings_section( \'section-one\', \'Section One\', \'section_one_callback\', \'my-plugin\' );
add_settings_field( \'field-one\', \'Field One\', \'field_one_callback\', \'my-plugin\', \'section-one\' );
<form method="post" action="options.php">
<?php if($active_tab == \'new\') {
settings_fields( \'my-plugin\' );
do_settings_sections( \'my-plugin\' );
} else if($active_tab == \'d_options\') {
// Do D_Options
} else if($active_tab == \'s_options\') {
// Do S_Options
}
submit_button();
?>
</form>