A couple months ago, I got my first WP plugin in the WP plugin repo. The plugin uses register_activation_hook
with an attached function to create some custom MySQL tables to store data.
Now that I'm adding some new features, I need to run some ALTER TABLE table_name ADD new_column
queries to add some new columns to the tables (for existing plugin installations).
I'm guessing I should use the upgrader_process_complete
hook to run the ALTER TABLE
queries?
I'm also guessing I will need to start storing the current plugin version number in the database using update_option('my_plugin_version')
and get_option('my_plugin_version')
to compare the installed plugin version and the current version available ?
Should I always have the current table structure used, ie:
$sql= "CREATE TABLE " . $table_name . " (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`field1` text NOT NULL,
`field2` text NOT NULL,
`field3` text NULL,
PRIMARY KEY (`id`)
) $charset_collate;";
in the register_activation_hook
and not just keeping the original table structure, then checking the version to apply the appropriate tables updates for each version number?