Skip to main content
use version_compare()
Source Link
shea
  • 5.6k
  • 4
  • 38
  • 62

No, the column must exist before you insert data into it. Otherwise the query will fail.

You should edit your table creation SQL query to accommodate the new column. Then, run it through dbDelta() again. dbDelta() will compare your query and the table structure and only create the missing columns.

The best way to tell if the database structure is up-to-date is to store the plugin version in an option and compare it against the current plugin version.

Example:

class Awesome_Plugin {

    /** current plugin version */
    public $version = 4'4.2;2';

    function __construct() {
        $this->create_table();
    }

    function create_table() {
        global $wpdb;
        $installed_version = get_option( 'awesome_plugin_version' );

        if ( $installed_verversion_compare( !==$installed_ver, $this->version, '!=' ) ) {

            $sql = "CREATE TABLE $table_name{$wpdb->prefix}awesome_plugin (
                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');php';
            dbDelta( $sql );

            update_option( 'awesome_plugin_version', $this->version );
        }
    }
}

$GLOBALS['awesome_plugin']$awesome_plugin = new Awesome_Plugin;Awesome_Plugin();

You can read more at the WordPress Codex

No, the column must exist before you insert data into it. Otherwise the query will fail.

You should edit your table creation SQL query to accommodate the new column. Then, run it through dbDelta() again. dbDelta() will compare your query and the table structure and only create the missing columns.

The best way to tell if the database structure is up-to-date is to store the plugin version in an option and compare it against the current plugin version.

Example:

class Awesome_Plugin {

    /** current plugin version */
    public $version = 4.2;

    function __construct() {
        $this->create_table();
    }

    function create_table() {
        global $wpdb;
        $installed_version = get_option( 'awesome_plugin_version' );

        if ( $installed_ver !== $this->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');
            dbDelta( $sql );

            update_option( 'awesome_plugin_version', $this->version );
        }
    }
}

$GLOBALS['awesome_plugin'] = new Awesome_Plugin;

You can read more at the WordPress Codex

No, the column must exist before you insert data into it. Otherwise the query will fail.

You should edit your table creation SQL query to accommodate the new column. Then, run it through dbDelta() again. dbDelta() will compare your query and the table structure and only create the missing columns.

The best way to tell if the database structure is up-to-date is to store the plugin version in an option and compare it against the current plugin version.

Example:

class Awesome_Plugin {

    /** current plugin version */
    public $version = '4.2';

    function __construct() {
        $this->create_table();
    }

    function create_table() {
        global $wpdb;
        $installed_version = get_option( 'awesome_plugin_version' );

        if ( version_compare( $installed_ver, $this->version, '!=' ) ) {

            $sql = "CREATE TABLE {$wpdb->prefix}awesome_plugin (
                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';
            dbDelta( $sql );

            update_option( 'awesome_plugin_version', $this->version );
        }
    }
}

$awesome_plugin = new Awesome_Plugin();

You can read more at the WordPress Codex

added example class
Source Link
shea
  • 5.6k
  • 4
  • 38
  • 62

No, the column must exist before you insert data into it. Otherwise the query will fail.

You should edit your table creation SQL query to accommodate the new column. Then, run it through dbDelta() again. dbDelta() will compare your query and the table structure and only create the missing columns.

The best way to tell if the database structure is up-to-date is to store the plugin version in an option and compare it against the current plugin version.

Example:

class Awesome_Plugin {

    /** current plugin version */
    public $version = 4.2;

    function __construct() {
        $this->create_table();
    }

    function create_table() {
        global $wpdb;
        $installed_version = get_option( 'awesome_plugin_version' );

        if ( $installed_ver !== $this->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');
            dbDelta( $sql );

            update_option( 'awesome_plugin_version', $this->version );
        }
    }
}

$GLOBALS['awesome_plugin'] = new Awesome_Plugin;

You can read more at the WordPress Codex

No, the column must exist before you insert data into it. Otherwise the query will fail.

You should edit your table creation SQL query to accommodate the new column. Then, run it through dbDelta() again. dbDelta() will compare your query and the table structure and only create the missing columns.

No, the column must exist before you insert data into it. Otherwise the query will fail.

You should edit your table creation SQL query to accommodate the new column. Then, run it through dbDelta() again. dbDelta() will compare your query and the table structure and only create the missing columns.

The best way to tell if the database structure is up-to-date is to store the plugin version in an option and compare it against the current plugin version.

Example:

class Awesome_Plugin {

    /** current plugin version */
    public $version = 4.2;

    function __construct() {
        $this->create_table();
    }

    function create_table() {
        global $wpdb;
        $installed_version = get_option( 'awesome_plugin_version' );

        if ( $installed_ver !== $this->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');
            dbDelta( $sql );

            update_option( 'awesome_plugin_version', $this->version );
        }
    }
}

$GLOBALS['awesome_plugin'] = new Awesome_Plugin;

You can read more at the WordPress Codex

Source Link
shea
  • 5.6k
  • 4
  • 38
  • 62

No, the column must exist before you insert data into it. Otherwise the query will fail.

You should edit your table creation SQL query to accommodate the new column. Then, run it through dbDelta() again. dbDelta() will compare your query and the table structure and only create the missing columns.