WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi a new version of a plugin I have in the repository changes the way the data is stored in the database, how can I perform a check on upgrade that automatically moves the data to the new location, so the users don't lose any of their settings and are not aware of what is happening beneath the hood?

share|improve this question
Here's some sort of relevant info clark-technet.com/2010/12/… – fdsa Aug 13 '12 at 1:29
up vote 2 down vote accepted
global $myplugin_db_version;
$myplugin_db_version = "1.0";

function myplugin_install() {
    global $wpdb;
    global $myplugin_db_version;

    $table_name = $wpdb->prefix . "myplugin";

    $installed_ver = get_option( "myplugin_db_version" );

    if( $installed_ver != $myplugin_db_version ) {

        $sql = "CREATE TABLE $table_name (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
                name tinytext NOT NULL,
                text text NOT NULL,
                url VARCHAR(100) DEFAULT '' NOT NULL,
                UNIQUE KEY id (id));

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        update_option( "myplugin_db_version", $myplugin_db_version );


function myplugin_update_db_check() {
    global $myplugin_db_version;
    if ( get_site_option( 'myplugin_db_version' ) != $myplugin_db_version) {
add_action('plugins_loaded', 'myplugin_update_db_check');
share|improve this answer
Does it make any difference if the db version is stored as a constant instead of a global variable? – drtanz Aug 13 '12 at 12:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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