最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - WordPress Fatal error: Uncaught Error: Call to undefined function dbDelta()

programmeradmin2浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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.

发布评论

评论列表(0)

  1. 暂无评论