How to add new options to my WordPress plugin

If you’re storing a set of options, or a single option with a schema, you should do the following:

  • keep a version number stored in the database, ideally as a separate value, though if you are using a single option with an array inside it, you could store it there
  • keep a version number in your plugin as a defined value in a file
  • on init or admin_init, compare the value in your plugin against the value in the database
    • if they differ, run an upgrade function

This way your plugins data gets updated, even if no update/activation/deactivation occurs. Your existing method would fail if an old backup was restored, or a plugin was manually updated via FTP. This is what plugins such as GravityForms do, and they take advantage of this system to implement multiple functions to upgrade from 2 -> 3 -> 4 etc

You can also use the PHP function version_compare to use version comparisons:

https://www.php.net/manual/en/function.version-compare.php

$version = ... get version from database;
if ( ! $version ) {
    // fresh install!
    do_first_time_install_of_marcos_plugin();
} else if ( version_compare( MARCOS_PLUGIN_VERSION, $version, '>') ) {
    // the plugin version is higher than what's in the DB!
    do_marcos_plugin_upgrade();
}