When Omeka detects that a user has installed a new version of the plugin, it automatically deactivates the plugin and presents an "Upgrade" button. This hook runs when a superuser clicks the "Upgrade" button. It is primarily used to migrate database tables and other data stored by older versions of the plugin.
Arguments
- string
$oldVersion
{{#if - | (optional)}}
- The previous version that was installed and is being upgraded from.
- {{#if: | Default:
{{{default}}}
}} - string
$newVersion
{{#if - | (optional)}}
- The new, current version of the plugin.
- {{#if: | Default:
{{{default}}}
}}
Example
The following example code has a switch statement, and based upon the previous plugin version, runs specific code to upgrade versions.
Notice that there are no break
statements inside the switch
. Omitting the switches allows the execution to "fall through"; once a matching old version is found, the upgrade code for that version and every subsequent version is executed. This means that you don't have to duplicate code for upgrading from various different versions.
add_plugin_hook('upgrade', 'myplugin_upgrade'); function myplugin_upgrade($oldVersion, $newVersion) { switch($oldVersion) { // let the plugin cascade its upgrades case '1.0': // code to upgrade from 1.0 to 1.1 case '1.1': // code to upgrade from 1.1 to 2.0 case '2.0': // code to upgrade from 2.0 to 2.1 } }