1

I have added a new field (time_last_seen) in my plugin (v1.1) table then uploaded my plugin as version 1.2 to wordpress svn repository. When I update my plugin from admin panel, it is not creating the field (time_last_seen) in the table.

Here is what I have tried:

function ulh_add_user_logins_table() {
    global $wpdb;
        $oldVersion = get_option( 'fa_userloginhostory_version', '1.0' );
        $newVersion = '1.2';

    $charset_collate = $wpdb->get_charset_collate();
        $fa_user_logins_table = $wpdb->prefix . "fa_user_logins";

    $sql = "CREATE TABLE $fa_user_logins_table (
         id int(11) NOT NULL AUTO_INCREMENT,
   user_id int(11) ,
  `time_login` datetime NOT NULL,
  `time_logout` datetime NOT NULL,
   `time_last_seen` datetime NOT NULL, 
  `ip_address` varchar(20) NOT NULL,
  `browser` varchar(100) NOT NULL,
  `operating_system` varchar(100) NOT NULL,
  `country_name` varchar(100) NOT NULL,
  `country_code` varchar(20) NOT NULL   ,                             
   PRIMARY KEY (`id`)
    ) $charset_collate;";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
    update_option( 'fa_userloginhostory_version', $newVersion );
}

register_activation_hook(__FILE__, 'ulh_add_user_logins_table');
2

register_activation_hook() only attaches a function to run on activation of your plugin, not on updating. See the docs for full details, particularly this part:

3.1 : This hook is now fired only when the user activates the plugin and not when an automatic plugin update occurs (#14915).

Of course, you could force your hook to run by deactivating and re-activating your plugin, but you certainly don't want your users to have to do this :)

A better way would be to manage the 'database version' of your plugin - so perhaps, store the 'current version' number of your plugin in the database. When your plugin runs, check this version number against the real version of your plugin. If it is ever different... that's when you want to add this field to the database.

Of course, for brand new installs of v1.1 of your plugin, you still want to run this as you are currently. You just need to also consider the upgrade path that existing users - such as yourself in this instance - will take.

Further reading which I would definitely recommend for this topic:

  • I have given the answer as per your suggestion. Can you tell if there is any issue in my code given in answer. – Faiyaz Alam May 22 '16 at 16:42
  • At a glance that looks good to me! Is it working for you? – Tim Malone May 22 '16 at 20:20
0

as per the answer given by Tim Malone, I have this working code:

 /* Activate Hook Plugin */
    register_activation_hook(__FILE__, 'ulh_add_user_logins_table');

    /* call when plugin is activated */
    function ulh_add_user_logins_table() {
        global $wpdb;
        $charset_collate = $wpdb->get_charset_collate();
            $fa_user_logins_table = $wpdb->prefix . "fa_user_logins";

        $sql = "CREATE TABLE $fa_user_logins_table (
             id int(11) NOT NULL AUTO_INCREMENT,
       user_id int(11) ,
      `time_login` datetime NOT NULL,
      `time_logout` datetime NOT NULL,
      `ip_address` varchar(20) NOT NULL,
      `browser` varchar(100) NOT NULL,
      `operating_system` varchar(100) NOT NULL,
      `country_name` varchar(100) NOT NULL,
      `country_code` varchar(20) NOT NULL   ,                             
       PRIMARY KEY (`id`)
        ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
            update_option( 'fa_userloginhostory_version', '1.0' );
            ulh_update_tables_when_plugin_updating();
    }


    //call when plugin is updated. 
//Actually it gets called every time 
//but the sql query execute only when there is plugin version difference
    function ulh_update_tables_when_plugin_updating() {
        global $wpdb;
            $oldVersion = get_option( 'fa_userloginhostory_version', '1.0' );
            $newVersion = '1.2';

            if ( !(version_compare( $oldVersion, $newVersion ) < 0) ) {
                return FALSE;
            }

        $charset_collate = $wpdb->get_charset_collate();
            $fa_user_logins_table = $wpdb->prefix . "fa_user_logins";

        $sql = "CREATE TABLE $fa_user_logins_table (
             id int(11) NOT NULL AUTO_INCREMENT,
       user_id int(11) ,
      `time_login` datetime NOT NULL,
      `time_logout` datetime NOT NULL,
       `time_last_seen` datetime NOT NULL,
      `ip_address` varchar(20) NOT NULL,
      `browser` varchar(100) NOT NULL,
      `operating_system` varchar(100) NOT NULL,
      `country_name` varchar(100) NOT NULL,
      `country_code` varchar(20) NOT NULL   ,                             
       PRIMARY KEY (`id`)
        ) $charset_collate;";

        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
        dbDelta( $sql );
        update_option( 'fa_userloginhostory_version', $newVersion );
    }

    add_action('init', 'ulh_update_tables_when_plugin_updating');

Hope this helps to other user.

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.