• Resolved Imagesamiur6688

    (@samiur6688)


    Hi ,I am trying to add a new field to an existing database when a plugin is updated. For example, I have a database that has some fields . When the plugin is updated to a new version from the dashboard, there will be another field added to the existing database. This should not affect the data stored in the other fields which were created during the previous versions installation. I am using dbDelta() . Can maybe_add_column() be used for this or is there another way to do this ? Thank you for your time and suggestion.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Imagethreadi

    (@threadi)

    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?

    Thread Starter Imagesamiur6688

    (@samiur6688)

    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 .

    Moderator Imagebcworkz

    (@bcworkz)

    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.

    Thread Starter Imagesamiur6688

    (@samiur6688)

    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.

    Moderator Imagethreadi

    (@threadi)

    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/

    Thread Starter Imagesamiur6688

    (@samiur6688)

    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.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Adding a new field in database upon plugin update’ is closed to new replies.