3

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

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.

2
  • The problem with running these updates on a per-site basis is then the plugins will be bloated with overhead for backwards compatibility. It'd have to constantly be checking the version number last recorded in the database, then have code for handling both the previous storage method as well as the new storage method. Jul 1, 2014 at 15:18
  • You can only execute this check if is an admin logged in and is in Panel (will reduce drastically the number of checks and allow you to set update warnings). Also, you can set the option to autoload (will be cached) when setting add_option. I didn't understand the backwards compatibility problem... Oct 11, 2017 at 12:48

Your Answer

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

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