我创建了一个插件,可以在不同的选项卡上配置设置。每个选项卡将其设置保存到wp_options
. 每个选项卡的HTML都被分离到自己的文件中,因此PHP和HTML的混合最少。
我想向现有选项卡添加其他字段,但我想将这些特定设置另存为wp_options
. 我知道这可以使用add_settings_field
并在回调函数中指定HTML,但我希望继续将HTML与PHP分开。
这两种方法都能做到吗?
Thx。
[更新]
partials/book-review-admin-advanced.php
:
<form action="options.php" method="post">
<?php
@settings_fields( \'advanced_options\' );
@do_settings_fields( \'advanced_options\' );
// This doesn\'t work:
// @settings_fields( \'general_options\' );
// @do_settings_fields( \'general_options\' );
// Neither does adding a separate form for general_options.
?>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<label for="book_review_api_key">
<?php esc_html_e( \'Google API Key\', $this->plugin_name ); ?>:
</label>
</th>
<td>
<input id="book_review_api_key" class="regular-text text-input" type="text"
name="book_review_advanced[book_review_api_key]"
value="<?php echo esc_attr( $advanced[\'book_review_api_key\'] ); ?>">
</td>
</tr>
<tr>
<th scope="row">
<label for="book_review_test">
<?php esc_html_e( \'Test Field\', $this->plugin_name ); ?>:
</label>
</th>
<td>
<!-- How can I save both book_review_advanced and book_review_general? -->
<input id="book_review_test" class="regular-text text-input" type="text"
name="book_review_general[test]">
</td>
</tr>
</tbody>
</table>
<?php @submit_button(); ?>
</form>
class-book-review-admin.php
:
public function init_menu() {
register_setting( \'advanced_options\', \'book_review_advanced\', array( $this, \'sanitize_advanced\' ) );
register_setting( \'general_options\', \'book_review_general\', array( $this, \'sanitize_general\' ) );
}
public function sanitize_advanced( $input = array() ) {
$output = array();
// Do some sanitization.
return $output;
}
public function sanitize_general( $input = array() ) {
$output = array();
// Do some sanitization.
return $output;
}
public function render_tabbed_content() {
if ( isset ( $_GET[\'tab\'] ) ) {
$active_tab = $_GET[\'tab\'];
}
else {
$active_tab = \'advanced\';
}
if ( $active_tab == \'advanced\' ) {
$advanced = get_option( \'book_review_advanced\' );
include_once( \'partials/book-review-admin-advanced.php\' );
}
// Handle other tabs.
}