How to test ‘upgrader_process_complete’ hook in plugin development?

OK, so you want to do something after update (let’s say add some DB table, change its structure or something like that).

The feature request you mention is very old and I don’t believe it would be solved – because there is no need for that feature.

There are many scenarios, when your plugin won’t be notified that its version changed:

  1. User manually uploads newer version and performs update.
  2. User goes to FTP and overwrites plugin directory with newer version.
  3. User deletes the plugin and uploads newer version.
  4. User used your plugin in old version some time ago and now uploads it again using FTP.

So what? All of these cases should cause update notification? No, I don’t believe so.

The update notification is an event that is meant to notify you, that plugin updater ended its job. You can log it, you can send some notification emails, and so on.

But you shouldn’t rely on it to perform any actions for newer version of a plugin. And these cases above show you why.

On the other hand – if your plugin won’t work, then it’s its fault, not WP. And when you go to Codex page for that hook, you’ll see:

The upgrader_process_complete action hook is run when the download
process for a plugin install or update finishes.

It is passed two arguments: an instance of the WP_Upgrader() class and
the $hook_extra array.

Use with caution: When you use the upgrader_process_complete action
hook in your plugin and your plugin is the one which under upgrade,
then this action will run the old version of your plugin.

So I don’t believe you should use this action in your case at all…

So how other plugins take care of that?

One of the common practice is versioning the DB. (WP also does it). You can put an option and store your plugin version in there. Your plugin may check that version and if it sees, that this option is different than its version, then DB update (or any other operations) has to be done.

tech