WordPress options page not saving

Question

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.

UPDATE


This 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.

0
Mr. Jo 2 years 2020-01-01T08:38:22-05:00 0 Answers 70 views 0

Leave an answer

Browse
Browse