I made a WordPress plugin which has all its options saved in the wp_options table as an array under a single option. Now I made a new version of the plugin with one new boolean option which is set with a checkbox on the plugin’s option page.
I want the option to be checked by default, which is no problem by fresh install of the plugin, but it is a problem by plugin upgrade.
The problem is, that the unchecked checkbox doesn’t set the option array key to false but removes it from the array alltogether, so I’m unable to distinguish whether the value has not yet been set or it was set to false by the user.
AFAIK there’s no action hook for plugin updates.
Is there an elegant and preferred way to set default values for new plugin options during the plugin’s update?
The proper way to handle an upgrade path is to only run an upgrade procedure when you need to. Ideally, you would store a âversionâ in your pluginâs database option, and then a version in the code. If they do not match, you would fire your upgrade procedure, and then set the database option to equal the version in the code. This is how many plugins handle upgrades, and this is how core works as well.