我正在学习如何创建 WordPress 插件。我想在 WordPress 数据库中创建一个表。但是在编写开发表站点的代码后显示 500 错误。我尝试使用 MySQL Workbench 创建表,但该站点无法正常工作。我尝试在其他功能中执行此操作并在插件激活时初始化该功能,但它没有起作用。这是创建表的代码:
function my_plugin_start(){
$status_url = get_option('');
preg_match('/^http(s)?\/\/[^\/]+\/(.*)$/i', $status_url, $matches);
$real_url = $_SERVER['REQUEST_URI'];
preg_match('/^\/(.+)(\?.+)$/i', $real_url, $uri_matches);
if ($uri_matches[1] == $matches[2]){
if(isset($_GET['dcode'])){
start_download();
}
else {
interkassa_process();
}
}
// The code for creating a table
global $wpdb;
$table_name = $wpdb->prefix . "my_product_base";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
UNIQUE KEY id (id) AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
cost VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL
);";
require_once(ABSPATH . 'cottage/wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
add_action('admin_menu', 'my_plugin_add_admin_menu');
add_action('init', 'my_plugin_start');
最佳答案
尝试修改后的代码
function my_plugin_start(){
$status_url = get_option('');
preg_match('/^http(s)?\/\/[^\/]+\/(.*)$/i', $status_url, $matches);
$real_url = $_SERVER['REQUEST_URI'];
preg_match('/^\/(.+)(\?.+)$/i', $real_url, $uri_matches);
if ($uri_matches[1] == $matches[2]){
if(isset($_GET['dcode'])){
start_download();
}
else {
interkassa_process();
}
}
// The code for creating a table
global $wpdb;
$table_name = $wpdb->prefix . "my_product_base";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
UNIQUE KEY id (id) AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
cost VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL
);";
require_once(ABSPATH . '/cottage/wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
add_action('admin_menu', 'my_plugin_add_admin_menu');
add_action('init', 'my_plugin_start');
关于mysql - 在 WordPress 数据库站点中创建表时出现 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50758553/