我正试图创建一个仅用于学习目的的插件,但我一直无法在wp数据库上创建表。
这是我的代码:
register_activation_hook( __FILE__, 'DBP_tb_create' );
register_activation_hook( __FILE__, 'DBP_install_data' );
function DBP_tb_create() {
global $wpdb;
//step1
$DBP_tb_name = $wpdb->prefix .'dbp_tb_login';
//step2
$charset_collate = $wpdb->get_charset_collate();
$DBP_query = "CREATE TABLE $DBP_tb_name(id int(10) NOT NULL AUTO_INCREMENT,
username varchar(255) DEFAULT '',age int(100) DEFAULT '',address varchar(255) DEFAULT'',PRIMARY KEY (id)) $charset_collate;";
//step3
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $DBP_query );
function DBP_install_data(){ 全局$ wpdb;
$welcome_name = 'Mr. WordPress';
$welcome_text = 'Congratulations, you just completed the installation!';
$table_name = $wpdb->prefix . 'liveshoutbox';
$wpdb->insert(
$table_name,
array(
'time' => current_time( 'mysql' ),
'name' => $welcome_name,
'text' => $welcome_text,
)
);
}
答案 0 :(得分:1)
这可能会帮助您:
<?php
register_activation_hook( __FILE__, 'install' );
global $plugin_name_db_version;
$plugin_name_db_version = '1.0.0';
function install(){
global $wpdb;
global $plugin_name_db_version;
$charset_collate = $wpdb->get_charset_collate();
$sql="
CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."plugin_name`
(
id bigint(20) NOT NULL auto_increment,
...
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql);
add_option( 'plugin_name_db_version', $plugin_name_db_version );
}
?>