我定制表不上的WordPress插件激活创建
问题描述:
我想在插件激活创建WordPress数据库自己的自定义表..This是我的代码..我定制表不上的WordPress插件激活创建
function __construct()
{
register_activation_hook(__FILE__,array(&$this, 'activate'));
function activate()
{
global $wpdb;
echo "<div class='updated'>Test Plugin Notice</div>";
$table_name = $wpdb->prefix . "dive";
$installed_ver = get_option("divebook_db_table_dive_version");
//Check if the table already exists and if the table is up to date, if not create it
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name
|| $installed_ver != $divebook_db_table_dive_version) {
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
date bigint(11) DEFAULT '0' NOT NULL,
site tinytext NOT NULL,
description text NOT NULL,
max_depth mediumint(9) NOT NULL,
time mediumint(9) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option("divebook_db_table_dive_version", $divebook_db_table_dive_version);
}
//Add database table versions to options
add_option("divebook_db_table_dive_version", $divebook_db_table_dive_version);
}
}
余米检查我的数据库。没有新的表格已经创建..Plz看着它..
答
这是因为任何WP激活/去激活挂钩需要从插件主文件内运行,而不是从主文件中包含的文件运行。 因此,尝试从plugin-name.php文件运行激活钩子,它将起作用。
L.E:
此外,我没有看到$ divebook_db_table_dive_version被定义。另一件事,dbDelta过去给我造成了一些问题,试试用$ wpdb-> query()来运行create table查询。
它全部写在我的主文件中...... – 2013-04-24 05:40:09
我假设'函数__construct()'是一个类的一部分,你使用'new'运算符实例化那个类,对吧? – Twisted1919 2013-04-24 05:43:13
也,我没有看到'$ divebook_db_table_dive_version'被定义。另一件事,dbDelta过去给我造成了一些问题,试着用'$ wpdb-> query()'来运行create table查询。 – Twisted1919 2013-04-24 05:46:10