**描述:**我的WordPress插件遇到了一个问题,激活后的表格创建过程无法按预期工作。以下是详细信息:
我尝试过:
1.验证SQL查询:已验证用于表创建的SQL查询在手动执行时是否有效。
1.使用echo
重新编译:添加了一个echo
语句,用于在激活过程中输出表名,以确保正确生成表名。
1.查看了调试日志:查看WordPress调试日志,查看激活过程中的任何错误。
**期望:**我期望表创建成功,echo
语句在调试日志中显示表名。
**实际结果:**没有创建表,echo
语句没有输出,调试日志也没有显示任何与创建表过程相关的错误。
**相关代码:**以下是我的代码的相关部分:
主文件(comment-reactions-plugin.php
):
// ... (plugin initialization code)
// Include plugin-setting.php and db.php
require plugin_dir_path(__FILE__).'inc/plugin-setting.php';
require plugin_dir_path(__FILE__).'inc/db.php';
字符串
db.php(包含文件):
// ... (error reporting and constant checks)
// Creating tables in WordPress
function wp_comment_reactions() {
global $wpdb;
$table_name = $wpdb->prefix . "comment_reactions_plugin";
echo "Table Name: $table_name"; // This line is not producing any output
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
user_id mediumint(9) NOT NULL,
post_id mediumint(9) NOT NULL,
like_count mediumint(9) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$result = dbDelta($sql);
if ($result === false) {
echo $wpdb->last_error; // This line does not produce any output in my case
}
}
register_activation_hook(__FILE__, 'wp_comment_reactions');
型
我很感激任何可能导致这个问题的见解。谢谢!
1条答案
按热度按时间guz6ccqo1#
如果echo没有输出,确保激活钩子被触发。
dbdelta函数总是很棘手。使用WordPress文档中的SQL来看看你的问题是否与SQL有关:
字符串
如果没有锁,尝试查询方法:
型