0

I'm looking to run some migration related code when my plugin is updated to the next release. I've researched and yet to find any sort of hook/trigger which will be called when a plugin has successfully upgraded.

All the suggestions I've found so far suggest to run migration on admin_init() and cross check a stored plugin version in options. But this seems like a really "hacky" solution and would imagine there must be a more structured way to run migrations (database related) when a plugin is updated from a pervious version.

What is the best way to trigger migration code for a plugin when it has been updated both manually and automatically?

1 Answer 1

0

plugin update in WordPress is basically removing it and installing again, so there is no special hook for this action.

So this hacky solution is the best one:

  • Save in options your last version
  • On init, check if current version is newer than saved version
  • If yes, perform your SQL actions
2
  • With the "On Init", surely that will require the user to visit the admin/settings page for the plugin otherwise the site could stay broken until they do if migration is breaking changes (ie: changing options names)
    – David
    Mar 29, 2023 at 9:40
  • Init is called at the beginning, before options are loaded, or content is printed: codex.wordpress.org/Plugin_API/Action_Reference It is safe to make such changes on init.
    – mWin
    Mar 30, 2023 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.