0

I am new to plugin develop and I have a problem with settings page of my new plugin.

My page seems to save correctly settings data to wordpress database, but after a few hours/days value stored on database disappear.

the code of the main page of the plugin is:

//security
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

//define path
define('ffita_gads_DIR', plugin_dir_path(__FILE__));
require_once(ffita_gads_DIR.'inc/settings.php');

//add shortcode
add_shortcode('ffita_gads', 'ffita_view_ads');


/* Runs on plugin deactivation*/
register_deactivation_hook( __FILE__, 'ffita_gads_remove' );

function ffita_gads_remove() {
    /* Deletes shortcodes */
    remove_shortcode ('ffita_gads');
}


//add menu 
add_action('admin_menu', 'ffita_gads_admin_menu');

function ffita_gads_admin_menu () {
    //  richiama la funzione ffi_gads_setting_page definita nel file settings.php
    add_menu_page( 'Impostazioni', 'FFI G Ads settings', 'manage_options', 'ffita_gads_option', 'ffi_gads_setting_page', 'dashicons-images-alt' ); 
}

The code of setting page is:

function ffi_gads_setting_page() {

    // security
    defined('ABSPATH') or die('No script kiddies please!');

    // verifica che l'utente possa gestire le impostazioni
    if (!current_user_can('manage_options')) {
        wp_die(__('You do not have sufficient permissions to access this page.', 'FFita G Ads'));
    }

    // form wordpress
    ?>

        <div class="wrap">
            <h1 class="dashicons-before dashicons-admin-settings">FFItalia Options</h1>
            <form name="ffiset_gads_form" method="post" action="options.php">

                <?php

                    // add_settings_section callback is displayed here. For every new section we need to call settings_fields.
                    settings_fields("ffita_gads_settings");

                    // all the add_settings_field callbacks is displayed here
                    do_settings_sections("ffita_gads_settings");

                ?>

                <table class="widefat" style="margin-top: 10px;">
                    <tr>
                        <td colspan="3"><h2>Connection parameters</h2></td>
                    </tr>
                    <tr>
                        <th scope="row" style="width: 15%;">ID VALUE</th>
                        <td style="vertical-align: middle; width: 45%;"><input type="text"
                            name="ffita_gads_capub"
                            value="<?php echo esc_attr( get_option('ffita_gads_capub') ); ?>"
                            style="width: 90%" required /></td>
                        <td>ID value" .</td>
                    </tr>
                    <tr>
                        <th scope="row" style="width: 15%;">IP debug</th>
                        <td style="vertical-align: middle; width: 45%;"><input type="text"
                            name="ffita_gads_ipdebug"
                            value="<?php echo esc_attr( get_option('ffita_gads_ipdebug') ); ?>"
                            style="width: 90%"></td>
                        <td>Ip for debug....</td>
                    </tr>

                </table>


                <table class="widefat Product" style="margin-top: 10px;">
                    <tr>
                        <td colspan="3"><h2>Parameter</h2></td>
                    </tr>
                    <tr>
                        <th scope="row" style="width: 10%;">ID</th>
                        <td style="width: 20%;">Product id</td>
                        <td>Product description</td>
                        <td>Shortcode</td>
                    </tr>


                    <?php

                        $Product_options = get_option('ffita_gads_option');
                        $num_Product = 0;

                        if (empty($Product_options)) {
                            $codice_html = "<tr><td colspan=4 bgcolor=red>Imposta i dettagli di almeno un Product</td></tr>";
                            $codice_html .= '<tr><td><input type="text" size="2" name="listaProduct[1][Product1][id_shortcode_Product]" value="1" readonly></td>';
                            $codice_html .= '<td><input type="text" name="listaProduct[1][Product1][id_Product]]" value="" required/></td>';
                            $codice_html .= '<td><input type="text" name="listaProduct[1][Product1][desc_Product]]" value="" size="60"/></td>';
                            $codice_html .= '<td>[ffita_gads id="1" ] </td></tr>';
                            echo $codice_html;
                            $num_Product++;
                        } else {
                            foreach ($Product_options as $dati_Product) {
                                $html = '<tr><td><input type="text" size="2" name="listaProduct[' . $dati_Product[id_shortcode_Product] . '][Product' . $dati_Product[id_shortcode_Product] . '][id_shortcode_Product]" value="' . $dati_Product[id_shortcode_Product] . '" readonly></td>';
                                $html .= '<td><input type="text" name="listaProduct[' . $dati_Product[id_shortcode_Product] . '][Product' . $dati_Product[id_shortcode_Product] . '][id_Product]]" value="' . $dati_Product[id_Product] . '" required/></td>';
                                $html .= '<td><input type="text" name="listaProduct[' . $dati_Product[id_shortcode_Product] . '][Product' . $dati_Product[id_shortcode_Product] . '][desc_Product]]" value="' . $dati_Product[desc_Product] . '" size="60"/></td>';
                                $html .= '<td> [ffita_gads id="' . $dati_Product[id_shortcode_Product] . '" ]</td></tr>';
                                echo $html;
                                $num_Product++;
                            }
                        }

                    ?>

                </table>

                <div align="right">
                    <input type="button" value="Add Product" id="add_ban_but"
                        data-value="<?php echo $num_Product ?>" /> <input type="button"
                        value="Remove last Product" id="remove_ban_but" />
                </div>

                <input type="text" id="n_tot" name="ffita_n_tot"
                    value="<?php echo $num_Product; ?>" />            

                <?php
                    // Add the submit button to serialize the options
                    submit_button();
                ?>

            </form>
        </div>

    <?php
}

if (isset($_POST['submit'])) {

    // registra i dati dei Product
    $nuovi_Product = $_POST['listaProduct'];
    $array_Product = array();

    // print_r ($nuovi_Product);

    foreach ($nuovi_Product as $key) {
        $nuovo_Product = $key;
        $array_Product = array_merge($array_Product, $nuovo_Product);
    }

    update_option('ffita_gads_option', $array_Product);

    // registra i dati ca-pub-xxxx
    $nuovo_ca = $_POST['ffita_gads_capub'];
    update_option('ffita_gads_capub', $nuovo_ca);

    // registra i dati ipdebug
    $nuovo_ipdeb = $_POST['ffita_gads_ipdebug'];
    update_option('ffita_gads_ipdebug', $nuovo_ipdeb);

}

function ffita_gads_option_init() {

    // registra il codice publisher ca-pub xxxx
    register_setting("ffita_gads_settings", "ffita_gads_capub");
    // delete_option("ffita_gads_capub");
}

add_action("admin_init", "ffita_gads_option_init");

Any idea of the problem? many thanks in advance

0

Your chunk of code if (isset($_POST['submit'])) { would process all the form submissions (even other forms which is not your settings page) which is why you are lossing your data. Even though this is not good way to create option page.

But

If you do something like

if(is_admin() && isset($_POST['submit']) && isset($_POST['ffita_gads_capub'])){....

could solve your problem.

  • it seems to work I check in the next days ... thanks – FFIta Apr 27 '18 at 9:55
0

The Settings API and Options API are same same but different :D . You cannot use OPTION field in SETTING form. SETTING form must have only setting field. If you want to use OPTION, please handle the OPTION outside the SETTING form.

Read more:

https://codex.wordpress.org/Settings_API

https://codex.wordpress.org/Options_API

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.