我已经在Wordpress中创建了一个选项页面。经过几个小时的工作,我很高兴能在“设置”页面上看到期望的内容。但是,当我按下保存按钮时,我将被重定向到Wordpress options.php,并且值没有按我期望的那样保存。这是我的代码:
<?php
################ Settings init ###################
/**
* Add new settings submenu to wordpress settings menu
*/
add_action( 'admin_menu', 'register_settings_submenu' );
function register_settings_submenu() {
add_submenu_page( 'options-general.php', 'Additonal Settings', 'Additonal Settings', 'manage_options', 'settings-submenu', 'settings_page' );
}
/**
* Settings tabs and content
*/
function settings_page() {
global $settings_active_tab;
$settings_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'payments'; ?>
<h2 class="nav-tab-wrapper">
<?php
do_action( 'payments_tab' )
?>
</h2>
<?php
do_action( 'payments_content' );
}
################ Payments section ###################
/**
* Payments tab
*/
add_action( 'payments_tab', 'payments_tab', 1 );
function payments_tab() {
global $settings_active_tab; ?>
<a class="nav-tab <?php echo $settings_active_tab === 'payments' || '' ? 'nav-tab-active' : ''; ?>"
href="<?php echo admin_url( 'admin.php?page=settings-submenu&tab=payments' ); ?>"><?php _e( 'Zahlungen ', 'woocommerce' ); ?> </a>
<?php
}
/**
* Payments content
*/
add_action( 'payments_content', 'payment_settings_element' );
function payment_settings_element() {
global $settings_active_tab;
if ( '' || 'payments' !== $settings_active_tab ) {
return;
}
settings_fields( 'payment_settings' );
require 'settings/payments.php'; //This includes the $settings array
createNewSettingsForm( 'Provision und Gebühren', $settings, 'payment_settings', 'payment-options' );
}
################### Functions to create a new settings entry ###################
function createNewSettingsForm( $title, $settings, $settings_fields, $settings_sections ) { ?>
<div class="wrap">
<h2><?php echo $title; ?></h2>
<form method="post" action="options.php">
<table class="form-table">
<tbody>
<?php
foreach ( $settings as $key => $setting ) {
add_settings_field( $key, $setting['name'], createNewSettingsEntry( $setting['name'], $setting['id'], $setting['type'], $setting['desc'] ), $settings_sections, $settings_fields );
register_setting( $settings_fields, $key );
} ?>
</tbody>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
/**
* Creates a new settings entry
*
* @param $name The name of the input
* @param $id The id of the input
* @param $type The type of the input
* @param $desc The description of the input
*/
function createNewSettingsEntry( $name, $id, $type, $desc ) {
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td class="forminp forminp-text">
<input type="<?php echo $type; ?>" name="<?php echo $id; ?>" id="<?php echo $id; ?>"
value="<?php echo get_option( $id ); ?>">
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php
}
为什么当我按下保存按钮时,我被重定向到选项页面?我的意思是正常情况下,当我按下按钮时,页面应该重新加载并显示保存的值。
更新这是我的数组的示例:
$settings = array(
'field1' => array(
'name' => __( 'Field 1', 'settings' ),
'id' => 'wc_field1',
'type' => 'text',
'desc' => __( '%', 'settings' )
),
'field2' => array(
'name' => __( 'Field 2', 'settings' ),
'id' => 'wc_field2',
'type' => 'text',
'desc' => __( '§', 'settings' )
)
);
如您所见,这是一个嵌套数组。