As mentioned, after_switch_theme
should be a pretty good choice. Your table create/update function will only run when you switch themes, which should be roughly like activating a plugin. If you use dbDelta
to create and/or update the table there should be no issue with table creation. From the Codex:
global $wpdb;
$installed_ver = get_option( "jal_db_version" );
if( $installed_ver != $jal_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' );
dbDelta( $sql );
update_option( "jal_db_version", $jal_db_version );
}
That will check the hard-coded table version against the version stored in the database and create or update if necessary. Note: it will create or update based on calculations made by dbDelta
. It will not try to create the same table twice. If the table already exists it will try to apply the differences between the existing table and the new table definition.