插件激活时不创建自定义表

时间:2018-01-16 作者:Felipe Gomes

我是插件开发的新手。我尝试了以下文档:https://codex.wordpress.org/Function_Reference/register_activation_hook

我在主插件文件(wp-content/plugin-folder/plugin.php)上创建了一个函数。它在插件激活时创建MySQL表。

CODE:

<?php
function create_db() {

 global $wpdb;

 $table_names = [
     $wpdb->prefix . "banks",
     $wpdb->prefix . "roles",
     $wpdb->prefix . "levels",
     $wpdb->prefix . "training_areas",
     $wpdb->prefix . "disciplines",
     $wpdb->prefix . "themes",
     $wpdb->prefix . "subjects",
     $wpdb->prefix . "fields",
     $wpdb->prefix . "modalities",
     $wpdb->prefix . "difficulties",
     $wpdb->prefix . "questions"
 ];

 $charset_collate = $wpdb->get_charset_collate();

 $sqls = [
     "CREATE TABLE $table_name[0](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[1](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[2](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[3](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[4](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[5](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     discipline_id INT(9) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id),
     FOREIGN KEY  (discipline_id) REFERENCES $table_name[4](id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[6](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[7](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[8](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[9](
     id INT(9) NOT NULL AUTO_INCREMENT,
     name VARCHAR(255) NOT NULL,
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id)
     ) $charset_collate;",

     "CREATE TABLE $table_name[10](
     id INT(9) NOT NULL AUTO_INCREMENT,
     code string NOT NULL,
     question VARCHAR(255)  NOT NULL,
     bank_id INT(9),
     role_id INT(9),
     level_id INT(9),
     training_area_id INT(9),
     theme_id INT(9),
     subject_id INT(9),
     field_id INT(9),
     modality_id INT(9),
     dificulty_id INT(9),
     time datetime DEFAULT \'0000-00-00 00:00:00\'  NOT NULL,
     PRIMARY KEY  (id),
     FOREIGN KEY  (bank_id) REFERENCES $table_name[0](id),
     FOREIGN KEY  (role_id) REFERENCES $table_name[1](id),
     FOREIGN KEY  (level_id) REFERENCES $table_name[2](id),
     FOREIGN KEY  (training_area_id) REFERENCES $table_name[3](id),
     FOREIGN KEY  (theme_id) REFERENCES $table_name[5](id),
     FOREIGN KEY  (subject_id) REFERENCES $table_name[6](id),
     FOREIGN KEY  (field_id) REFERENCES $table_name[7](id),
     FOREIGN KEY  (modality_id) REFERENCES $table_name[8](id),
     FOREIGN KEY  dificulty_id) REFERENCES $table_name[9](id),
     ) $charset_collate;"
  ];

  require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');

  foreach ($sqls as $sql) {
     dbDelta($sql);
  }
}

register_activation_hook(__FILE__, \'create_db\');

1 个回复
SO网友:Felipe Gomes

抱歉,伙计们,我创建了一个数组$table\\u name,并在sql语句中使用了$table\\u name。

谢谢

结束

相关推荐

WordPress中的AJAX,将坐标数据发送到MySQL并显示为MAP

我花了几个月的时间试图弄清楚如何获取用户的地理位置并将其传递到MySQL,以及在地图上用标记显示后如何将其传递到MySQL。我已经在通过ajax将数据传递给php方面遇到了困难。并在stackoverflow中进行搜索和研究。看来我的代码是正确的。AJAX似乎有一种与WORDPRESS协作的特殊方式。下面是中存在的代码template-maps.php: var pos; var infoWindow = new google.maps.InfoWindow({map: map});