Imagine a WordPress MultiSite Network with plugin Foo in it (but only active on some of the sites). When upgrading plugin Foo from version 1.0 to 2.0, Foo would like to do some work in the database. (For the sake of this exercise, let’s just assume it wants to stop storing data in 10 different wp_options rows, and store it all in a serialized array.)

How can Foo run its foo_db_update script, for each site, when Foo is updated?

A common answer is that Foo should hook into admin_init, and check the current version of Foo against the db-stored version, and see if it needs to run this script. The primary problem here is that it’s not something that can be easily done in a MultiSite environment. This approach might work if I could easily trigger the admin_init events of all sites in the network – with each site’s current plugins loaded – but I don’t know a way to do that.

I’ve tried hooking into wpmu_upgrade_site, and running Upgrade Network after updating Foo to version 2.0. However, that only uses switch_to_blog to act as each site, and doesn’t fully load the plugins for each site it’s switching to. This means that if Foo adds a function to wpmu_upgrade_site, that function won’t fire when Upgrade Network is clicked from the Network Admin.

So, what IS the best way for a plugin to run such a script in a MultiSite environment?

1 Answer
1

It’s is good idea to run similar DB updates per site basis. You can store plugins version in wp_options (which are options for current site), then on admin_init compare version and run upgrade.

If you want to run more complicated/big updates, I would recommend creating custom upgrade page for plugin and only display admin notice for users, so they can run it manually.

Leave a Reply

Your email address will not be published. Required fields are marked *