Last active
April 23, 2018 16:11
-
-
Save mikeyhoward1977/ba254b43ddeceac35e6dac9724c36f54 to your computer and use it in GitHub Desktop.
WordPress Plugin Upgrade (Basic)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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