• Hello

    I have a problem with the validation of input from an options page I receive by using the settings API. I want to save the content of two text-input fields INTPUT as key-value pairs in the Table Options. To do this, I added 2 Settings Fields, and write these entries in the Validation function in the desired shape in an array. Here is the code of the validation function:

    function mu_file_types_validate($input){
        $options = get_option('mu_file_types') ;
        $key = trim($input['abbreviation']) ;
        $options[$key] = trim($input['mime']) ;
        return $options ;
    }

    This works quite nice, as long as there is already an option saved. Is this not the case, and there is no entry, the problems occur. If I, for example, insert the pair a, b, I get an error message:

    Notice: Undefined index: abbreviation in C:\Program Files\xampp\xampp\htdocs\DorfNeu\wp-content\plugins\myUploads\adminMenue.php on line 97

    Notice: Undefined index: mime in C:\Program Files\xampp\xampp\htdocs\DorfNeu\wp-content\plugins\myUploads\adminMenue.php on line 98

    These are the indices of the array ‘input’ in the fct ‘mu filetypes validate’ that is passed as parameter. If I now reload the page, then there is a new array (mu file types) in the options tyble, but with 2 entries of the form: (””=>”” , ”a” => ”b” ). At first, always an empty entry exists. Now you can easily add more pairs, they are all stored properly.
    As I understand, the parameter ‘input’ gets its content from the post variable, and this doesn’t depend on the content of the Table Options. So I do not know, where to start here, to eliminate the error.

    Here is the whole code of the Option Page:

    <?php
    add_action('admin_menu', 'myUploads_menu' ) ;    // Bindet das Menue ein
    add_action('admin_init', 'mu_init_settings'); // Initialisiert die Settings
    /*
     * Im Backend soll eine Liste aller gespeicherten Dateitypen angezeigt werden, mit der Möglichkeit,
     * einen Typ für den Upload zuzulassen, bzw ihn zu sperren.
     * Des weiteren soll die Möglichkeit bestehen, weitere Dateitypen, d.h. Mime Typ und Dateierweiterung
     * anzugeben.
     * Die Dateitypen werde in einem Array file_types nach dem Schema (Dateierweiterung => Mime-Type)
     * in der Tabelle Options gespeichert.
     * Ein weiteres array permitted_mime_types speichert die Dateierweiterungen der Zugelassenen Dateitypen.
     */
    
    function myUploads_menu(){
        /*
         * Die wrapper-fkt 'add_options_page' fügt eine neue Admin-Seite Einstellungen hinzu, die über
         * das Einstellungen->'2.Parameter' (hier myUploads) angesteuert werden kann
         */
        add_options_page('myUploads Einstellungen' , 'myUploads' , 'manage_options',
                         'myUploadOptions' , 'mu_settngs_menu') ;
    }
    
    /**
     * Erzeugt das Options-Menü im Backend
     */
    function mu_settngs_menu(){
        if(!current_user_can('manage_options')){
            wp_die( __('You do not have the sufficient permissons to access this page.')) ;
        }?>
        <div class="wrap"><!-- Inhalt der Klasse wrap wird mit dem WordPress Look and Feel ausgestattet -->
            <h2>Einstellungen des Plugins myUploads</h2>
            <form action="options.php" method="post" >
                <?php settings_fields('mu_file_types') ?>
                <?php do_settings_sections('myUploadOptions')?>
                <input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes') ?>" />
            </form>
        </div><!-- end class="wrap" -->
        <?php
    }
    /**
     * registriert die Settings?
     * Die Fkt sird bei admin_init ausgeführt, sie legt die Settings für das Plugin fest.
     *
     */
    function mu_init_settings() {
        //register our settings
        register_setting( 'mu_file_types' , 'mu_file_types' , 'mu_file_types_validate' );
    
        add_settings_section('file_types' , 'The File Types' , 'file_type_section_callback' ,'myUploadOptions' ) ;
    
        add_settings_field('id_abbreviation_key' , 'Dateierweiterung' ,
                            'abbreviation_input_callback' , 'myUploadOptions' , 'file_types') ;
        add_settings_field('id_mime_value' , 'Mime Typ' ,
                            'mime_input_callback','myUploadOptions' , 'file_types') ;
    }
    /**
     * Erzeugt erste Section. registriert von der Fkt add_settings_field in make_settings_clear
     */
    function file_type_section_callback(){
        ?> <p>Mime Typen und deren Dateierweiterungen</p><?php
    }
    /**
     * Erzeugt das erste settings field.
     * registriert von add_settings_field in der fkt make_settings_clear
     */
    function abbreviation_input_callback(){
        $options = get_option('mu_file_types') ;
        if($options){
            foreach ($options as $key => $value){
                ?><p>Dateierweiterung : <?php echo $key ; ?> Mime Typ : <?php echo $value ; ?></p>
                <?php
            }
        }
        ?>
        <input id="id_abbreviation_key"  name="mu_file_types[abbreviation]"
                size="40" type="text" value="Schlüssel hier eintragen" />
        <?php
    }
    /**
     * Erzeugt eine Eingabemaske zur speicherung von Dateierweiterung und Mime Typ in einem Array
     * in der Form (Dateierweiterung => Mime )
     * Dateierweiterung und Mime Typ übergeben
     */
    function mime_input_callback(){
        $options = get_option('mu_file_types') ;
        ?>
        <input id="id_mime_value"  name="mu_file_types[mime]"
                size="40" type="text" value="<?php //echo $options['second_option'] ?>" />
        <?php
    }
    /**
     * kann bei register_settings mit aufgerufen werden
     * @param $input
     */
    function mu_file_types_validate($input){
        $options = get_option('mu_file_types') ;
        $key = trim($input['abbreviation']) ;
        $options[$key] = trim($input['mime']) ;
        return $options ;
    }

    Thanks

The topic ‘Settings API, empty entry in Options-Table causes error’ is closed to new replies.