我已经创建了具有这些设置的插件,然后我想用相同的表单复制其他选项卡的设置,但输入/输出过程将彼此独立。
以下是迄今为止的代码:
function __construct( $prefix )
{
$this->setttings_prefix = $prefix;
if( wp_verify_nonce($_POST[\'save_settings_field\'], \'save_settings_action\') )
{
$options = array();
foreach( $_POST as $key=>$value )
{
$options[$key] = $value;
}
update_option( $this->setttings_prefix.\'_options\', $options );
$this->message = \'<div class="alert alert-success">Settings saved</div>\';
}
}
function show_settings()
{
$current_page = isset($_GET[\'page\']) ? $_GET[\'page\'] : \'\';
$active_tab = isset( $_GET[ \'tab\' ] ) ? $_GET[ \'tab\' ] : \'settings1\';
?>
<div class="wrap tw-bs4">
<h2 class="nav-tab-wrapper">
<a href="?page=wsr_settings&tab=settings1" class="nav-tab <?php echo $active_tab == "settings1" ? "nav-tab-active" : ""; ?>"><?php _e(\'Settings1\', \'sc\'); ?></a>
<a href="?page=wsr_settings&tab=settings2" class="nav-tab <?php echo $active_tab == "settings2" ? "nav-tab-active" : ""; ?>"><?php _e(\'Settings2\', \'sc\'); ?></a>
</h2>
<hr/>
<?php
if( $active_tab == \'settings1\' )
{
?>
<form class="form-horizontal" method="post" action="">
<?php
wp_nonce_field( \'save_settings_action\', \'save_settings_field\' );
$config = get_option( $this->setttings_prefix.\'_options1\');
?>
<fieldset>
<?php
foreach( $this->setttings_parameters as $single_page )
{
$hidden_class = \'hidden\';
if($single_page[\'menu_slug\'] == $current_page)
{
$hidden_class = \'\';
}
foreach( $single_page[\'parameters\'] as $key=>$value )
{
switch( $value[\'type\'] )
{
case "separator":
$out .= \'
<div class="lead">\'.$value[\'title\'].\'</div>
\';
break;
case "text":
$out .= \'
<div class="form-group \'.$hidden_class.\'">
<label class="control-label" for="\'.$value[\'id\'].\'">\'.$value[\'title\'].\'</label>
<input type="text" class="form-control \'.$value[\'class\'].\'" name="\'.$value[\'name\'].\'" id="\'.$value[\'id\'].\'" placeholder="\'.$value[\'placeholder\'].\'" value="\'.esc_html( stripslashes( $config[$value[\'name\']] ) ).\'">
<p class="help-block">\'.$value[\'sub_text\'].\'</p>
</div>
\';
break;
case "hidden":
$out .= \'<input type="hidden" name="\'.$value[\'name\'].\'" value="\'.esc_html( stripslashes( $config[$value[\'name\']] ) ).\'">\';
break;
case "button":
$out .= \'
<div class="form-group \'.$hidden_class.\'">
<label class="control-label" for=""> </label>
<a class="btn btn-success" href="\'.$value[\'href\'].\'" >\'.$value[\'title\'].\'</a>
</div>
\';
break;
$out .= \'
</fieldset>
</div>
\';
break;
}
}
}
echo $out;
?>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save Settings</button>
</div>
</fieldset>
</form>
<?php
echo $this->message;
?>
</div>
<?php
}
elseif( $active_tab == \'settings2\' )
{
.......Same as above......
}
}
}
代码成功地显示了多个选项卡和表单,但仍然为每个设置选项卡显示相同的设置和I/O。请帮帮我。提前感谢。
SO网友:Drmzindec
基本上,您可以为每个“选项卡”添加一个标识符,然后将其保存在多维数组中,这是一个输入可以做到的巧妙技巧,对于使用选项卡的WordPress插件非常有用。
<form id="form_1">
<div class="tab_1">
<input type="text" name="tab1[\'fieldname_0\']" value="tab_1_0">
<input type="text" name="tab1[\'fieldname_1\']" value="tab_1_1">
<input type="text" name="tab1[\'fieldname_2\']" value="tab_1_2">
<input type="text" name="tab1[\'fieldname_3\']" value="tab_1_3">
</div>
<div class="tab_2">
<input type="text" name="tab2[\'fieldname_0\']" value="tab_2_0">
<input type="text" name="tab2[\'fieldname_1\']" value="tab_2_1">
<input type="text" name="tab2[\'fieldname_2\']" value="tab_2_2">
<input type="text" name="tab2[\'fieldname_3\']" value="tab_2_3">
</div>
</form>
您的PHP不需要更改,但在调用时返回数据
get_option()
将更改为一个数组,该数组很容易从那里迭代或通过$key引用。