我已经在Wordpress中创建了一个选项页。工作了几个小时后,我很高兴能在设置页面上看到预期的内容。但当我按下保存按钮时,我会被重定向到Wordpress选项。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
}
为什么当我按下保存按钮时会重定向到选项页?我的意思是,通常当我按下按钮时,页面应该重新加载并显示保存的值。
UPDATE
这是我的数组的外观示例:
$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\' )
)
);
如您所见,这是一个嵌套数组。
SO网友:sandrodz
你需要注意$page 参数,如果它在任何地方都不相同,则它将不起作用。
<?php add_settings_field( $id, $title, $callback, $page, $section, $args ); ?>
$页
(string)(必选)显示此字段的菜单页。Should match $menu_slug from add_theme_page() or from
do_settings_sections().
https://codex.wordpress.org/Function_Reference/add_settings_field
<?php add_settings_section( $id, $title, $callback, $page ); ?>
$页
(字符串)(必需)显示此节的菜单页。Should match $menu_slug from Function Reference/add theme page 如果要将节添加到“外观”页面,或者如果要将节添加到“设置”页面,则为“功能参考/添加选项”页面。
https://codex.wordpress.org/Function_Reference/add_settings_section
<?php add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
$菜单\\u段塞
(字符串)(必需)引用此菜单的段塞名称(对于此菜单应是唯一的)。
https://codex.wordpress.org/Function_Reference/add_options_page
在以下方面也应相同:
<?php settings_fields( $page ); ?>
<?php do_settings_sections( $page ); ?>
<?php register_setting( $page, $id ); ?>