Skip to content

Instantly share code, notes, and snippets.

@mikeyhoward1977
Last active April 23, 2018 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeyhoward1977/ba254b43ddeceac35e6dac9724c36f54 to your computer and use it in GitHub Desktop.
Save mikeyhoward1977/ba254b43ddeceac35e6dac9724c36f54 to your computer and use it in GitHub Desktop.
WordPress Plugin Upgrade (Basic)
<?php
/**
* Runs during admin_init hook to determine if our plugin has been upgraded.
*
* Determine if any processes need to be executed and call them as required.
*
* - Replace 'mp_plugin_version' with the option name used to store your plugin version in the DB
* - Replace 'mp_plugin_version_upgraded_from' with the option name you want to use to store the version upgraded from
* - Replace 'MP_PLUGIN_VERSION' with the constant you use to store your plugin version
*/
function mp_plugin_upgrade_procedure() {
$did_upgrade = false; // Tells us whether or not an upgrade was performed
$current_version = preg_replace( '/[^0-9.].*/', '', get_option( 'mp_plugin_version' ) );
// Compare the version in the DB with our plugin version constant to determine if we need to run the upgrade
if ( version_compare( $current_version, MP_PLUGIN_VERSION, '<' ) ) {
// Let us know that an upgrade has happened
$did_upgrade = true;
}
if ( $did_upgrade ) { // If an upgrade was performed
update_option( 'mp_plugin_version_upgraded_from', $current_version );
update_option( 'mp_plugin_version', preg_replace( '/[^0-9.].*/', '', MP_PLUGIN_VERSION ) );
}
}
add_action( 'admin_init', 'mp_plugin_upgrade_procedure' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment