admin管理员组文章数量:1302406
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: /
Description: This Test Plugin.
Version: 1.0
Author: Test
Author URI: /
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 ?
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 ?
Share Improve this question edited Jan 8, 2018 at 8:50 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jan 8, 2018 at 8:44 developermedeveloperme 1415 silver badges18 bronze badges 5- What in this code is not working? – kero Commented Jan 8, 2018 at 8:45
- Database not creating when activating the Plugin.And also database not deleting at the time of plugin deletion. – developerme Commented Jan 8, 2018 at 8:46
- 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. – Satyendra Prakash Commented Jan 8, 2018 at 11:12
- 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. – wpdev Commented Aug 8, 2019 at 18:24
- Related: Create a table in custom plugin on the activating it? – brasofilo Commented Jun 12, 2021 at 23:48
3 Answers
Reset to default 1Use 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' ) );
My suggestion is to register the activation and deactivation hook outside the class definition.
/*
* 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
}
}
Now in your plugin's main file
function activate_my_plugin() {
My_plugin_activator::activate();
}
function deactivate_my_plugin() {
My_plugin_deactivator::deactivate();
}
Now register your function to the activation/deactivation hook.
register_activation_hook( __FILE__, 'activate_my_plugin' );
register_deactivation_hook( __FILE__, 'deactivate_my_plugin' );
though i was just trying to create table and the table was not created because i was not selecting the database name in query .after adding the name ,it worked perfectly the code is following :
$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() . ";";
本文标签:
版权声明:本文标题:How to Create database table when Plugin installedactivated, and delete database when Plugin deleted 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741681174a2392177.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论