文件wp-admin/includes/upgrade.php
包括wp-admin/includes/schema.php
. 顶部声明为全局(see source):
// Declare these as global in case schema.php is included from a function.
global $wpdb, $wp_queries, $charset_collate;
...
$charset_collate = \'\';
if ( ! empty( $wpdb->charset ) )
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty( $wpdb->collate ) )
$charset_collate .= " COLLATE $wpdb->collate";
所以你可以按照@Toscho的答案进行检查
$wpdb
. 或者,以托肖为例:
global $wpdb, $charset_collate;
require_once ABSPATH . \'wp-admin/includes/upgrade.php\';
$sql = "CREATE TABLE $table (
ID bigint unsigned NOT NULL auto_increment,
event_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
event_group tinytext,
event_title text,
PRIMARY KEY (ID)
) $charset_collate;";
dbDelta( $sql );
请注意
IF NOT EXISTS
不需要
as dbDelta()
handles this.