如何在插件安装/激活时创建数据库表,在插件删除时删除数据库

时间:2018-01-08 作者:developerme

我正在尝试用面向对象编程创建新的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");
} 
//    
}
你能帮帮我吗?

3 个回复
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\' );

SO网友:Mitaly Tabassum

虽然我只是尝试创建表,但由于我没有在查询中选择数据库名称,所以没有创建表。添加名称后,它工作得很好。代码如下:

  $query="CREATE TABLE `jobs`.`$dbp_tb_name` (
     `id` INT NOT NULL ,
     `pass` VARCHAR(10) NOT NULL ,
     `name` VARCHAR(30) NOT NULL ,
     `phn` INT NOT NULL
      ) ENGINE = InnoDB;" . $wpdb->get_charset_collate() . ";";

结束

相关推荐

Metaboxes in Loop

我想在foreach循环中创建元盒。要求是,我有一个自定义帖子类型的数组,我想运行一个循环,为作为数组中键的帖子类型添加元盒。到目前为止,我已经开发了很多需求,但现在我被困在回调函数所在的部分add_meta_box() 将被调用。我用过call_user_func_array 调用回调函数并传递$key 作为参数,但它返回错误且不起作用。下面是我的代码:function rttk_create_boxes(){ $myarray = $this->rttk_get_posttype