我正在创建oop基础插件。所以,如何在插件安装过程中在a侧类中创建表。但我无法创建表。这是我的方法。我怎么了。
class Notification
{
function __construct() {
register_activation_hook( __FILE__, array($this,\'jal_install\') );
}
function jal_install()
{
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . \'fnotice\';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id INT NOT NULL AUTO_INCREMENT,
msg varchar(500) NOT NULL,
time DATETIME NOT NULL,
type varchar(350) NOT NULL,
link varchar(350) NOT NULL,
status ENUM(\'0\', \'1\') NOT NULL default \'1\',
PRIMARY KEY (id) )";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
add_option( \'jal_db_version\', $jal_db_version );
}
}
$obj=new notification;
最合适的回答,由SO网友:Karun 整理而成
您应该运行register_activation_hook
创建对象后。
class Notification
{
function jal_install()
{
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . \'fnotice\';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id INT NOT NULL AUTO_INCREMENT,
msg varchar(500) NOT NULL,
time DATETIME NOT NULL,
type varchar(350) NOT NULL,
link varchar(350) NOT NULL,
status ENUM(\'0\', \'1\') NOT NULL default \'1\',
PRIMARY KEY (id) )";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
add_option( \'jal_db_version\', $jal_db_version );
}
}
$obj=new Notification();
register_activation_hook( __FILE__, array($obj,\'jal_install\') );