I've create an option page in Wordpress. After some hours of work I was happy that I can see my expected content on my settings page. But when I press the save button I'm getting redirected to the Wordpress options.php and the values are not saving like I expect it. This is my code:
<?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
}
Why I'm getting redirected to the options page when I press the save button? I mean normally when I press the button the page should reload and show the saved values.
UPDATEThis is an example how my array looks like:
$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' )
)
);
As you can see this is a nested array.
I've create an option page in Wordpress. After some hours of work I was happy that I can see my expected content on my settings page. But when I press the save button I'm getting redirected to the Wordpress options.php and the values are not saving like I expect it. This is my code:
<?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
}
Why I'm getting redirected to the options page when I press the save button? I mean normally when I press the button the page should reload and show the saved values.
UPDATEThis is an example how my array looks like:
$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' )
)
);
As you can see this is a nested array.
Share Improve this question edited Nov 1, 2018 at 0:33 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Oct 31, 2018 at 21:54 Johnny97Johnny97 2147 silver badges18 bronze badges1 Answer
Reset to default 1You need to pay attention to $page parameter, if its not same anywhere it will not work.
<?php add_settings_field( $id, $title, $callback, $page, $section, $args ); ?>
$page
(string) (required) The menu page on which to display this field. Should match $menu_slug from add_theme_page() or from do_settings_sections().
https://codex.wordpress/Function_Reference/add_settings_field
<?php add_settings_section( $id, $title, $callback, $page ); ?>
$page
(string) (required) The menu page on which to display this section. Should match $menu_slug from Function Reference/add theme page if you are adding a section to an 'Appearance' page, or Function Reference/add options page if you are adding a section to a 'Settings' page.
https://codex.wordpress/Function_Reference/add_settings_section
<?php add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?>
$menu_slug
(string) (required) The slug name to refer to this menu by (should be unique for this menu).
https://codex.wordpress/Function_Reference/add_options_page
And it should also be same in:
<?php settings_fields( $page ); ?>
<?php do_settings_sections( $page ); ?>
<?php register_setting( $page, $id ); ?>