I am working to develop a plugin that needs to create a table in database here is my code please help the code is not showing any errors and not creating the table
public function create_phone_order_db() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'phone_orders';
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
id INT NOT NULL AUTO_INCREMENT,
billing_country VARCHAR(100) NOT NULL,
billing_first_name VARCHAR(50) NOT NULL,
billing_last_name VARCHAR(50) NOT NULL,
billing_company VARCHAR(50) NOT NULL,
billing_address_1 VARCHAR(120) NOT NULL,
billing_address_2 VARCHAR(120) NOT NULL,
billing_city VARCHAR(50) NOT NULL,
billing_state VARCHAR(40) NOT NULL,
billing_postcode VARCHAR(8) NOT NULL,
billing_email VARCHAR(25) NOT NULL,
billing_phone VARCHAR(20) NOT NULL,
ship_to_different_address VARCHAR(10) NOT NULL,
shipping_country VARCHAR(50) NOT NULL,
shipping_first_name VARCHAR(40) NOT NULL,
shipping_last_name VARCHAR(40) NOT NULL,
shipping_company VARCHAR(50) NOT NULL,
shipping_address_1 VARCHAR(120) NOT NULL,
shipping_address_2 VARCHAR(120) NOT NULL,
shipping_city VARCHAR(50) NOT NULL,
shipping_state VARCHAR(50) NOT NULL,
shipping_postcode VARCHAR(8) NOT NULL,
admin_email_switched VARCHAR(25) NOT NULL,
admin_switched_id INT NOT NULL,
admin_user_name_switched VARCHAR(30) NOT NULL,
admin_role_switched VARCHAR(15) NOT NULL,
order_comments VARCHAR NOT NULL,
shipping_method VARCHAR NOT NULL,
payment_method VARCHAR NOT NULL,
_wpnonce VARCHAR NOT NULL,
_wp_http_referer VARCHAR NOT NULL,
PRIMARY KEY (id)
) " . $charset_collate . ";";
dbDelta($sql);
}
register_activation_hook( FILE, array($this, 'create_phone_order_db'));
答案 0 :(得分:1)
根据WordPress Codex你应该做
class MyPlugin {
static function install() {
global $wpdb;
$table_name = $wpdb->prefix . 'my_table';
$charset_collate = $wpdb->get_charset_collate();
$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(55) DEFAULT '' NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
}
register_activation_hook(__FILE__, array( 'MyPlugin', 'install' ) );
您可以在创建有关插件激活的表格时阅读更多here。