On plugin activation hook I want to create a table but the following code is giving Uncaught Error
. I just wonder why it is not giving this error since I am properly including update.php
.
global $wpdb;
$sql = 'CREATE TABLE IF NOT EXISTS '.$wpdb->prefix.'gs_orders(';
$sql .= 'id BIGINT(20) AUTO_INCREMENT NOT NULL,';
$sql .= 'order_number VARCHAR(200) NOT NULL,';
$sql .= 'group_id INT NOT NULL,';
$sql .= 'group_admin BIGINT(20) NOT NULL,';
$sql .= 'group_user BIGINT(20) NOT NULL,';
$sql .= 'product_groups VARCHAR(255),';
$sql .= 'products VARCHAR(255),';
$sql .= 'created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,';
$sql .= 'updated_at DATETIME NULL DEFAULT NULL,';
$sql .= 'PRIMARY KEY (id),';
$sql .= 'INDEX (order_number, group_id, group_admin, group_user),';
$sql .= ')' . $wpdb->get_charset_collate() . ';';
require_once ABSPATH . '/wp-admin/includes/update.php';
dbDelta($sql);
Above code is on the plugin activation callback function.
On plugin activation hook I want to create a table but the following code is giving Uncaught Error
. I just wonder why it is not giving this error since I am properly including update.php
.
global $wpdb;
$sql = 'CREATE TABLE IF NOT EXISTS '.$wpdb->prefix.'gs_orders(';
$sql .= 'id BIGINT(20) AUTO_INCREMENT NOT NULL,';
$sql .= 'order_number VARCHAR(200) NOT NULL,';
$sql .= 'group_id INT NOT NULL,';
$sql .= 'group_admin BIGINT(20) NOT NULL,';
$sql .= 'group_user BIGINT(20) NOT NULL,';
$sql .= 'product_groups VARCHAR(255),';
$sql .= 'products VARCHAR(255),';
$sql .= 'created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,';
$sql .= 'updated_at DATETIME NULL DEFAULT NULL,';
$sql .= 'PRIMARY KEY (id),';
$sql .= 'INDEX (order_number, group_id, group_admin, group_user),';
$sql .= ')' . $wpdb->get_charset_collate() . ';';
require_once ABSPATH . '/wp-admin/includes/update.php';
dbDelta($sql);
Above code is on the plugin activation callback function.
Share Improve this question asked Aug 24, 2019 at 8:05 pixelngrainpixelngrain 1,3901 gold badge23 silver badges50 bronze badges1 Answer
Reset to default 0I was including the wrong file. Instead of update.php
I should include upgrade.php
. I know it's a silly mistake but maybe help someone who does the same in future.