我正在尝试用面向对象编程创建新的WordPress插件。我想在插件激活时创建数据库,在插件删除时删除数据库。下面是我的代码,它对我不起作用。我有两个文件,一个是主插件文件,另一个是插件函数包含的文件。
主要文件代码如下:
<?php
/*
Plugin Name: Test Reviews1
Plugin URI: https://test.in/
Description: This Test Plugin.
Version: 1.0
Author: Test
Author URI: https://test.in/
License: GPLv2 or later
*/
new test_plugin();
class test_plugin{
public function __construct(){
$this->plugin_dir = plugins_url( \'\' , __FILE__ );
include(\'inc/inc.php\');
$this->security = new hidemysite_security();
}
}
包括以下文件代码:
<?php
class hidemysite_security{
public function __construct() {
if (is_admin()) {
register_activation_hook(__FILE__, array(&$this, \'activate\'));
register_deactivation_hook( __FILE__, \'my_plugin_remove_database\' );
}
}
public function activate() {
global $wpdb;
$table = $wpdb->prefix . \'md_things\';
$charset = $wpdb->get_charset_collate();
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT \'\' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
}
public function my_plugin_remove_database() {
global $wpdb;
$table_name = $wpdb->prefix . \'md_things\';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
//delete_option("jal_db_version");
}
//
}
你能帮帮我吗?
SO网友:mukto90
改用此代码-
class hidemysite_security{
public function __construct() {
if (is_admin()) {
register_activation_hook(__FILE__, array( $this, \'activate\'));
register_deactivation_hook( __FILE__, array( $this, \'my_plugin_remove_database\' ) );
}
}
public function activate() {
global $wpdb;
$table = $wpdb->prefix . \'md_things\';
$charset = $wpdb->get_charset_collate();
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT \'0000-00-00 00:00:00\' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT \'\' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );
}
public function my_plugin_remove_database() {
global $wpdb;
$table_name = $wpdb->prefix . \'md_things\';
$sql = "DROP TABLE IF EXISTS $table_name";
$wpdb->query($sql);
//delete_option("jal_db_version");
}
//
}
这两条线已修改-
register_activation_hook(__FILE__, array( $this, \'activate\'));
register_deactivation_hook( __FILE__, array( $this, \'my_plugin_remove_database\' ) );
SO网友:Suraj Khanal
我的建议是在类定义之外注册激活和停用挂钩。
/*
* Fired during plugin activation.
**/
class My_plugin_activator() {
public static activate() {
// write your db code here
}
}
。
/*
* Fired during plugin deactivation.
**/
class My_plugin_deactivator() {
public static deactivate() {
// write your code here
}
}
现在在插件的主文件中
function activate_my_plugin() {
My_plugin_activator::activate();
}
function deactivate_my_plugin() {
My_plugin_deactivator::deactivate();
}
现在将您的函数注册到激活/停用挂钩。
register_activation_hook( __FILE__, \'activate_my_plugin\' );
register_deactivation_hook( __FILE__, \'deactivate_my_plugin\' );