0
add_action( 'admin_menu', 'fhwtest_plugin_menu' );

function fhwtest_plugin_menu() {
    add_menu_page( 'Reservierungs-Einstellungen', 'Reservierung', 'administrator', 'fhwtest_reservoptions', 'fhwtest_plugin_options_seite' );
    add_action( 'admin_init', 'fhwtest_plugin_options' );
}

function fhwtest_plugin_options() {
    register_setting( 'fhwtest_settings_group1', 'fhwtest_bahnzahl' );
    register_setting( 'fhwtest_settings_group1', 'fhwtest_email' );
    register_setting( 'fhwtest_settings_group1', 'fhwtest_datenbankname' );
}

function fhwtest_plugin_options_seite() {
    if ( !is_admin() ) {
        wp_die( __( 'Keine Berechtigung' ) );
    }
?>
    <style>
        fieldset { 
            border: 1px solid black; 
            padding: 15px; 
        }
        legend { font-weight: bold; }
    </style>
    <div class="wrap">
        <h1>Reservierungs-Einstellungen</h1>
        <form method="post" action="options.php">
        <?php settings_fields( 'fhwtest_settings_group1' ); ?>
        <?php do_settings_sections( 'fhwtest_settings_group1' ); ?>
            <fieldset>
                <legend>Bahnen</legend>
                <label>Anzahl Bahnen: 
                    <input type="number" value="<?php echo esc_attr( get_option( 'fhwtest_bahnzahl' ) ); ?>" name="bahnzahl" />
                </label>
            </fieldset>
            <fieldset>
                <legend>Benachrichtigung</legend>
                <label>E-Mail an 
                    <input name="email" value="<?php echo esc_attr( get_option( 'fhwtest_email' ) ); ?>" />
                </label>
            </fieldset>
            <fieldset>
                <legend>Datenbank</legend>
                <label>Datenbankname
                    <input name="email" value="<?php echo esc_attr( get_option( 'fhwtest_datenbankname' ) ); ?>" />
                </label>
            </fieldset>
        <?php submit_button(); ?>       
    </div>

<?php
}
?>

The site is correct, but if I type in values and save it, all the fields are empty again. Why aren't they saved?

5
  • Without looking your code: check the database for your meta data. Was it saved? Or you just can't get it? HTML is a cutie pie but brings nothing to solve a problem. – Max Yudin May 24 '17 at 11:35
  • @MaxYudin Its not saved in the database, the "fhwtest_bahnzahl", "fhwtest_email", "fhwtest_datenbankname" are existing, but there's no option_value. Is the option_id relevant? Should it be ascending? There are some counting fails. – John_H_Smith May 24 '17 at 11:42
  • You have to find a way to save it to the database. There are hundreds of tutorials around. This site is not a tutorial. – Max Yudin May 24 '17 at 11:49
  • @MaxYudin I know. As said in the question before you edited it, it was working. I was using this codex.wordpress.org/Creating_Options_Pages. I used google since one hour and can't find the problem while comparing my code with the one in the tutorials. – John_H_Smith May 24 '17 at 11:52
  • It was working, now it's not. Figure out the change you've made before things happened. P.S. I did not edit the post, I just removed incomprehensible and useless information which does not help to solve the problem. – Max Yudin May 24 '17 at 12:05
0

A few things to note in your code:

  1. Two inputs share the same name of "email", so when those values get sent in the form submission, the second one will overwrite the first.

  2. The input name attribute value should match the name of the option being saved. Instead of "email", use "fhwtest_email". Without that, the values won't be saved to the correct options.

  3. get_option() will return false if there's not a value set for the requested option name; if you pass false to esc_attr() it'll return an empty string, so the next save will set the value to an empty string.

While there are certainly lots of ways to approach the creation of settings, I suggest taking a look at the Plugin Developer's Handbook section on Settings for sample code and details into the Settings and Options APIs.

0

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.