• Anonymous User

    (@anonymized-9643630)


    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.

Viewing 10 replies - 1 through 10 (of 10 total)
  • popper

    (@julialasarte)

    See DbDelta() ??

    Thread Starter Anonymous User

    (@anonymized-9643630)

    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?

    popper

    (@julialasarte)

    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.

    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.

    Thread Starter Anonymous User

    (@anonymized-9643630)

    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?

    Thread Starter Anonymous User

    (@anonymized-9643630)

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

    popper

    (@julialasarte)

    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”

    Thread Starter Anonymous User

    (@anonymized-9643630)

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

    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.

    Thread Starter Anonymous User

    (@anonymized-9643630)

    Thanks Knut, you are very clear.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to update a database table during plugin update’ is closed to new replies.