1

I have a plugin with an activation hooked function. This function has to register 4 setting variables.

I can't see why, but only the first setting is registered correctly, the last 3 are somehow skipped.

This is the plugin main file:

<?php
/*
Plugin Name:  Integrazione Liferay
Version:      1.1.0
Text Domain:  integrazione-liferay
*/

register_activation_hook( __FILE__, 'integrazione_liferay_activate');
register_deactivation_hook( __FILE__, 'integrazione_liferay_deactivate');
register_uninstall_hook( __FILE__, 'integrazione_liferay_uninstall');

//Funzione di attivazione plugin
function integrazione_liferay_activate() {
    // Registro variabile che indica se il cron deve essere attivo
    if (get_option('integrazione_liferay_cron_enabled') === false) {
        $args = array(
            'type' => 'integer', 
            'default' => 0,
        );
        register_setting( 'integrazione_liferay_options', 'integrazione_liferay_cron_enabled', $args ); 
    }
    update_option( 'integrazione_liferay_cron_enabled', 1);

    // Registro variabile che indica se è necessario riscrivere i permalinks
    if (get_option('integrazione_liferay_flush_permalinks') === false) {
        $args = array(
            'type' => 'integer', 
            'default' => 0,
        );
        register_setting( 'integrazione_liferay_options', 'integrazione_liferay_flush_permalinks', $args ); 
    }
    update_option( 'integrazione_liferay_flush_permalinks', 0);

    // Registro variabile che indica l'ultimo timestamp elaborato
    if (get_option('integrazione_liferay_last_timestamp') === false) {
        $args = array(
            'type' => 'integer', 
            'default' => 0,
        );
        register_setting( 'integrazione_liferay_options', 'integrazione_liferay_last_timestamp', $args ); 
    }
    update_option( 'integrazione_liferay_last_timestamp', 0);

    // Registro variabile che memorizza il csv serializzato
    if (get_option('integrazione_liferay_csv_data') === false) {
        $args = array(
            'type' => 'string', 
            'default' => "",
        );
        register_setting( 'integrazione_liferay_options', 'integrazione_liferay_csv_data', $args ); 
    }
    update_option( 'integrazione_liferay_csv_data', "");
}

// Funzione di disattivazione plugin
function integrazione_liferay_deactivate() {
    // Disabilito creazione nuovi cron
    update_option( 'integrazione_liferay_cron_enabled', 0);
    // De-schedulo tutti i cron già schedulati
    $timestamp = wp_next_scheduled( 'integrazione_liferay_cron_hook' );
    while ($timestamp) {
        wp_unschedule_event( $timestamp, 'integrazione_liferay_cron_hook' );
        $timestamp = wp_next_scheduled( 'integrazione_liferay_cron_hook' );
    }
}

// Funzione di disinstallazione plugin
function integrazione_liferay_uninstall() {
    // Eseguo disattivazione preventiva
    integrazione_liferay_deactivate();
    // Rimuovo i settings
    unregister_setting( 'integrazione_liferay_options', 'integrazione_liferay_cron_enabled' ); 
    unregister_setting( 'integrazione_liferay_options', 'integrazione_liferay_flush_permalinks' ); 
    unregister_setting( 'integrazione_liferay_options', 'integrazione_liferay_last_timestamp' ); 
    unregister_setting( 'integrazione_liferay_options', 'integrazione_liferay_csv_data' ); 
}

I tried debugging the piece by deactivating and re-activating the plugin. The debugger actually tries to set the last 3 settings each time as get_option() on those 3 will always return false on every activation. This is the debugging result (GIF):

debugging

And this is the resulting /options.php after the activation:

options.php

I have used this settings pattern in some other plugins but I can't wrap my head around this one's behavior.

Thanks in advance for your replies.

2
  • 1
    I don't think you've understood what register_setting does. It's supposed to be run in admin_init, not just on activation, and is for building options pages with the Settings API. Jul 30, 2018 at 10:49
  • @JacobPeattie Thanks for pointing this out. It might be an unproper usage of settings, but nonetheless I would expect them to set. Anyway I admit I'm quite rookie at this so, would using options suitable for my purpose?
    – GigiSan
    Jul 30, 2018 at 11:00

1 Answer 1

5

register_setting does not insert anything in the database. It's supposed to be run in admin_init, not just on activation, and is for building options pages with the Settings API and handles things like sanitisation and permissions when saving options in the admin.

If you want to add some default option values to the database on activation, use add_option(). It will add the option and value to the database, but only if there isn't already a value for that option:

function integrazione_liferay_activate() {
    add_option( 'integrazione_liferay_cron_enabled', 1 );
    add_option( 'integrazione_liferay_flush_permalinks', 0 );
    add_option( 'integrazione_liferay_last_timestamp', 0 );
    add_option( 'integrazione_liferay_csv_data', '' );
}
2
  • Thank you very much. I will try this straight away. Still, I'm very puzzled about why did the first value insert.
    – GigiSan
    Jul 30, 2018 at 12:07
  • Perfect! Anyway, according to the codex, update_option() should have created the values anyway... But since the whole piece was kinda bogus I guess we'll never know what happened. Thanks again! ☺
    – GigiSan
    Jul 30, 2018 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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