【问题标题】:WordPress table creation throwing errors (and not creating tables)WordPress 表创建抛出错误(而不是创建表)
【发布时间】:2012-08-14 07:02:25
【问题描述】:

我对 WordPress 非常陌生,并且编写一个插件(理想情况下)允许我们的客户在一个地方更新他们现有的 WP 网站和我们 iphone 应用程序上的动态信息。这将需要创建两个表。我已经编写了我认为应该创建这些表的内容,但激活插件会在我的测试 WordPress 网站上引发此错误:

The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

查看数据库还显示未创建表。我在这里和其他地方查看了一些其他帖子,但似乎无法确定我哪里出错了。有什么建议吗?:

<?php
// Plugin info omitted

register_activation_hook(_FILE_, 'event_manager_install');
register_deactivation_hook(_FILE_, 'event_manager_uninstall');

// Installer
function event_manager_install() {
global $wpdb;

// Builds queries for custom event and speakers tables
$event_table = $wpdb->prefix . 'events';
$eventSql = "CREATE TABLE $event_table (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  venueName VARCHAR(250) DEFAULT '' NOT NULL,
  date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  address text NOT NULL,
  registrationDeadlineDate datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  UNIQUE KEY id (id)
);";

$speaker_table = $wpdb->prefix . 'speakers';
$speakerSql = "CREATE TABLE $speaker_table (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  eventID mediumint(9) NOT NULL,
  speaker text NOT NULL,
  UNIQUE KEY id (id)
);";

// Executes queries
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($eventSql);
dbDelta($speakerSql);
}

// Uninstaller
function event_manager_uninstall() {
global $wpdb;

// Creates queries to delete custom tables
$event_table = $wpdb->prefix . 'events';
$speaker_table  = $wpdb->prefix . 'speakers';
$eventSql = "DROP TABLE " . $event_table . ";";
$speakerSql = "DROP TABLE " . $speaker_table . ";";

// Executes deletion queries
$wpdb->query($eventSql);
$wpdb->query($speakerSql);
}
?>

【问题讨论】:

    标签: php mysql wordpress plugins


    【解决方案1】:

    问题是您拼错了__FILE__ 常量的名称。只需将_FILE_ 替换为__FILE__(注意:两个下划线),它应该可以正常工作。

    【讨论】:

    • 刚刚调整,同样的错误,仍然没有创建表。虽然没有注意到这一点,但超级大脸。还有其他想法吗?
    • 这很奇怪,我实际上在我的本地 WordPress 安装(WP 3.5-alpha)上测试了你的代码,它确实创建了表。您的错误日志中是否有任何消息?
    • 结束标签后面的空格。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 2016-04-07
    相关资源
    最近更新 更多