1

I am trying to create new WordPress plugin with object oriented programming.I want to create database when plugin activation and delete database when plugin deleting.Below is my code its not working for me.I have two files one is main plugin file other one is plugin functions included files.

main file code as below:

<?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();



        }

    }

include file code as below:

<?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");
} 
//    
}

Could You Please Help Me ?

  • What in this code is not working? – kero Jan 8 '18 at 8:45
  • Database not creating when activating the Plugin.And also database not deleting at the time of plugin deletion. – developerme Jan 8 '18 at 8:46
  • Did you try wppb.io (plugin boilerplate) because it includes all useful actions. Ie. action when activate or deactivate. Btw you can use uninstall.php for catching uninstall action. Simple just put dele_option or smt. – bilimokur Aug 8 at 18:24
0

What error are you getting?

Enable debuging in your wp-config.php file by changing define( 'WP_DEBUG', false ) to define( 'WP_DEBUG', true ) and see error message. It will help you finding the actual issue.

  • There is no error message is showing – developerme Jan 8 '18 at 11:15
  • check if your 'create table statement' is working . – Satyendra Prakash Jan 8 '18 at 11:28
0

Use this code instead-

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");
    } 
//    
}

These 2 lines were modified-

register_activation_hook(__FILE__, array( $this, 'activate'));
register_deactivation_hook( __FILE__, array( $this, 'my_plugin_remove_database' ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.