多站点上的注册激活挂钩

时间:2014-09-02 作者:aifrim

I am working on creating a plugin for a multisite wordpress framework that is enabled by default for all (by me, the super administrator)

When it activates it is required to create 2 tables where it will store some precedural information. Right now I am working on creating the first (most important) table.

The p_install function is called because of the options I set at the end of the function.

global $wpdb;
global $p_db_version;
$table_name = $wpdb->prefix . \'p_table\';
$wpdb->p_table = $wpdb->prefix . \'p_table\';
$charset_collate = \'\';
if (!empty($wpdb->charset)) {
    $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
}
if (!empty($wpdb->collate)) {
    $charset_collate .= " COLLATE {$wpdb->collate}";
}

$sql = "CREATE TABLE {$table_name} ( id mediumint(9) NOT NULL AUTO_INCREMENT, blog_id mediumint(9) NOT NULL, create_date datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL, modify_date datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, created_by mediumint(9) NOT NULL, modified_by mediumint(9) NOT NULL, is_active tinyint(1) DEFAULT 0 NOT NULL, p tinytext DEFAULT \'\' NOT NULL, v mediumint(9) DEFAULT 0 NOT NULL, c mediumint(9) DEFAULT 0 NOT NULL, json_data text NOT NULL, type mediumint(9) DEFAULT 0 NOT NULL, UNIQUE KEY id (id) ) {$charset_collate};";

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

delete_option(\'p_db_version\'); delete_option(\'p_db_sql\'); delete_option(\'p_db_timestamp\');

add_option(\'p_db_version\', $p_db_version); add_option(\'p_db_sql\', $sql); add_option(\'p_db_timestamp\', date(\'l jS \\of F Y h:i:s A\'));

But it fails to create the table. Please advise.

Edit 1

After reading PatJ\'s answer I feel the need to clean up some air:

  1. I got my table creation code from the dbDelta() documentation
  2. I have edited my code and used the two space policy

Answer

And the answer is that a datetime cant have the CURRENT_TIMESTAMP default value since MySQL 5.6.5 so I changed modify_date from datetime to timestamp and all is good.

Credits to: sebthebert

Thank you.

2 个回复
最合适的回答,由SO网友:aifrim 整理而成

答案是datetime 不能有CURRENT_TIMESTAMP 默认值自MySQL 5.6.5 所以我改变了modify_date 从…起datetimetimestamp 一切都很好。

SO网友:Pat J

dbDelta() documentation:

然而,请注意,dbDelta函数相当挑剔。例如:

您必须在SQL语句中将每个字段放在自己的行上

  • You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
  • 必须使用关键字key而不是其同义词索引,并且必须至少包含一个关键字(重点是我的。)

    我没看到一个PRIMARY KEY 在您的$sql 代码——也许这就是缺少的。

    结束

    相关推荐

    Multisite and plugins options

    我需要更多关于get_site_option(), update_site_option() 和add_site_option().在codex中,他们说它与单次安装相同,只是在多站点中,它返回网络范围的选项。好吧,但我对这一点感到困惑:这是否意味着一旦设置了网络范围选项,所有站点的选项都是相同的?还是每个站点都会覆盖它?我的意思是如果我这样做: update_site_option(\'option_group\', \'default_options());// default_options() r