Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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' ) );
// Check if the version being upgraded from is lower than 1.1 and if so, run some additional code
if ( version_compare( $kbs_version, '1.1', '<' ) ) {
$post_ids = get_posts( array(
'status' => 'draft',
'type' => array( 'post', 'page' ),
'posts_per_page' => -1,
'fields' => 'ids'
) );
foreach( $post_ids as $post_id ) {
add_meta_key( $post_id, '_mp_plugin_upgrade_example', 'Mikes Plugins Example' );
}
}
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
You can’t perform that action at this time.