1

I am writing plugin. I have added 1 custom table in database.

Suppose my plugin's current version is 1.0 After few days i am upgrading plugin and now its version 1.2. Same for after some time and now version is 1.3 and ..... 1.4

Now for every release of plugin version I have made some changes in database.

Than how can i upgrade database. I know about store db version in options table.

But i don't know because some user update his plugin from 1.1 -> 1.2, other is from 1.1 -> 1.4......... some is 1.2 -> 1.4..etc.. Than there is only way to add all conditions to code or any other solution is available?????

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.