WordPress plugin DB upgrade

In short, no. But it’s not particularly complicated. Just add a new version_compare block each time you revise the database:

$db_version = get_option( 'my_plugin_version' );

if ( version_compare( $db_version, '1.1', '<' ) ) {
    // Initial update code added in 1.1
}

if ( version_compare( $db_version, '1.2', '<' ) ) {
    // Additional update code added in 1.2
}

if ( version_compare( $db_version, '1.3', '<' ) ) {
    // And so forth
}

update_option( 'my_plugin_version', '1.3' );

This accounts for upgrades from any previous version, and only applies updates required to bring the db to the current version.

Leave a Comment