0

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

You 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.org/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.org/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.org/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 ); ?>
  • And what wrong with my function now? :| I'm not so good in PHP so I can't understand it so good. – Mr. Jo Nov 1 '18 at 0:36
  • @Mr.Jo Your code is poorly formatted and is very hard to read. So you have to go through it yourself and find where you've missed $page. In 99% of cases $page is the reason why Settings API won't save. – sandrodz Nov 1 '18 at 9:05
  • This is the official Wordpress code style. I've imported it into my IDE :| – Mr. Jo Nov 1 '18 at 11:20
  • By the way I've tried to set the page slug to every field but it's still not workin – Mr. Jo Nov 1 '18 at 11:44
  • @Mr.Jo wpcs has nothing to do with readability of your code. read this: hbfs.wordpress.com/2009/10/20/code-style-vertical-vs-horizontal – sandrodz Nov 1 '18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.