WordPress.org

Ready to get started?Download WordPress

Forums

How to update a database table during plugin update (11 posts)

  1. danycode
    Member
    Posted 2 years ago #

    Hallo, i want to add a new column in a database table of my plugin during the plugin update.

    Which is the proper hook to do that please? I can't find a solution on google.

  2. popper
    Member
    Posted 2 years ago #

    See DbDelta() :)

  3. danycode
    Member
    Posted 2 years ago #

    I'm looking for a hook that works as trigger of the database update.

    My question is not "how to modify a database" but "how to modify a database when a user update the plugin to the next release."

    There are no hooks associated with the plugin update?

  4. popper
    Member
    Posted 2 years ago #

    Ah, sorry I misunderstood.

    But no, as far as I know, there's no hooks associated with plugins update. This was discussed a while ago, but the devs decided not to implement it.

  5. Knut Sparhell
    Member
    Posted 2 years ago #

    Since you can't be sure the activation hook fires when the plugin is automatically updated, you should always check the need for updating the custom tables belong to your plugin (table or collection of tables).

    You do this most efficient by storing the plugin version, or "table collection" version, in an option. If this is lower than the current version hard-coded in your plugin, then do the db update, and finally, on success, update the option. This procedure could be called through the "init" hook.

    You should keep all update procedures, and loop from the previous version to the current version, to get all the updates in the right order. For this to be clean and simple, the database version should be an integer that increments by one for each update.

  6. danycode
    Member
    Posted 2 years ago #

    My idea is to store the database version in an option but the problem is:

    "when i can check the database version and update it?"

    Do this check every "admin_init" or "admin_head" execution is not a stupid thing?

  7. danycode
    Member
    Posted 2 years ago #

    So the activation hook fires when the plugin is updated or not?

  8. popper
    Member
    Posted 2 years ago #

    Not. According to the documentation:

    This hook is now fired only when the user activates the plugin and not when an automatic plugin update occurs

    And yeah, you could add an action to admin init to check if the version stored in you db is lower than the version of your code and then run an upgrade routine. here -at the bottom- there's some recommendations for plugins that do this "the right way"

  9. danycode
    Member
    Posted 2 years ago #

    Thanks, i'm going to use the init hook to do the update.

  10. Knut Sparhell
    Member
    Posted 2 years ago #

    I would prefer the "init" hook over "admin_init" because, in principle, you may have a plugin that updates other plugins without any user intervention. To be sure the updated plugin never acts on an outdated table structure, every page load should trigger the check for update, and eventually do it, before the rest of WordPress and your plugin executes.

  11. danycode
    Member
    Posted 2 years ago #

    Thanks Knut, you are very clear.

Topic Closed

This topic has been closed to new replies.

About this Topic