Are you talking about adding a new column to an individual database table when the plugin is updated? That should already be possible with dbDelta(). However, the challenge for you might be when this dbDelta() is executed. How do you currently check whether the plugin has been updated?
Yes I am referring to adding a new column to an individual database table when a new version of a plugin is released and updated from the WordPress dashboard. I executed dbDelta() in install.php. The following logic was used to check whether the plugin is updated.
function plugin_check_version() {
if ( $current_version !== PLUGIN_VERSION ) {
plugin_install();
}
}
add_action( ‘plugins_loaded’, ‘plugin_check_version’ ). This too was used in install.php .
Are you referring to the core /wp-admin/install.php file? That only executes when WP is first installed, before the user has installed any plugins. You also shouldn’t modify core files.
Use an appropriate action hook such as ‘automatic_updates_complete’ and/or one added via register_activation_hook()
Where are you adding this column? I strongly advise against modifying core DB table schema, but you can alter a table created by your plugin all you want.
I am not working on the core one /wp-admin/install.php . I used an install.php file within my plugin. It was there where I had executed the codes when I was facing a lot of problem. Turns out it is was not the right location. I am currently using plugins_loaded in my main plugin file. I have progressed more after considering the location of relevant code execution. Thanks.
I have necessary updates checked via the init hook. My function there usually looks like this one here: https://github.com/threadi/external-files-in-media-library/blob/master/app/Plugin/Update.php#L65
I.e. I compare the version number of the currently running plugin with the one I have saved in the database. If these differ, I perform the update tasks. Finally, I update the database value with the new version number.
Within this process, you can easily execute a dbDelta() to update database tables. Note that you should use the complete CREATE statement for this, as described in the manual: https://developer.wordpress.org/reference/functions/dbdelta/
Thanks for your time and consideration threadi and bcworkz . I think I have solved it. Just some further testing is left though . My mistake was calling plugins_loaded at the wrong file and did not remove get_option values upon uninstallation of the plugin. A set value for database version was probably preventing the conditional statemen from getting executed.