insert table in WordPress database - php

I want to insert a table into WordPress database while plugin activated. I have used this code but it is not working. This code should create a table while plugin gets activated, but in my WordPress when I activate the plugin, the code works, but table is not being created in the database.
How can I create the table in WordPress database while plugin activated?
I have use the below code for my plugin
class createtabel {
function __construct() {
register_activation_hook( __FILE__, 'create_plugin_database_table' );
}
public static function create_plugin_database_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'wp_productqa';
$sql = "CREATE TABLE $table_name (
QA_id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
Product_ID varchar(50) NOT NULL,
Username varchar(50)NOT NULL,
Useremail varchar(50) NOT NULL,
comment varchar(255) NOT NULL,
PRIMARY KEY (QA_id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
}

You have to make sure register_activation_hook is called.
For instance, in your plugin definition file you may:
$createTable = new createtabel;
register_activation_hook(__FILE__, array($createTable, 'create_plugin_database_table'));
BTW, in the createtabel class you can create your table with:
$wpdb->query($sql);
Is that your intention also to have
$table_name = $wpdb->prefix . 'wp_productqa';
which in most likelihood (assuming prefix is wp_)will have:
$table_name = 'wp_wp_productqa';

Please use this code while activate your plugin it will create new table in your database.
register_activation_hook(__FILE__, 'install_covercarousel');
Define this above hook in globally where your file coding has been started.
Also please dont' use public static function just add function and functioname like this function abs(){}
After changed code look like this
register_activation_hook( __FILE__, 'create_plugin_database_table' );
function create_plugin_database_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'wp_productqa';
$sql = "CREATE TABLE $table_name (
QA_id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
Product_ID varchar(50) NOT NULL,
Username varchar(50)NOT NULL,
Useremail varchar(50) NOT NULL,
comment varchar(255) NOT NULL,
PRIMARY KEY (QA_id)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}

Related

wordpress plugin activate/deactivate function not firing

I have to do some catalog plugin for wordpress but I am already having troubles with only activating / deactivating it. Here is my code. It should work. This code is only testing code for activate / deactivate, but considering the documentation, this should work and it seems I cannot find reasonable argument, why this is not firing. Can someone be so kind and take a look at the code? Thanks in advance
<?php
register_activation_hook( __FILE__, 'plus8k_activate' );
register_deactivation_hook( __FILE__, 'plus8k_deactivate' );
function plus8k_activate()
{
global $wpdb;
$table_name = $wpdb->prefix . "plus8k_products";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
description text NOT NULL,
meta tinytext NOT NULL,
content text NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
status bool DEFAULT 'true' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
$table_name = $wpdb->prefix . "plus8k_media";
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name tinytext NOT NULL,
caption text NOT NULL,
location tinytext NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
prod_id mediumint(9) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (prod_id)
REFERENCES " . $wpdb->prefix . "plus8k_products(id)
ON DELETE CASCADE
) $charset_collate;";
dbDelta( $sql );
}
function plus8k_deactivate()
{
global $wpdb;
$table_name = $wpdb->prefix . "plus8k_products";
$charset_collate = $wpdb->get_charset_collate();
$sql = "DROP TABLE $table_name";
dbDelta( $sql );
$table_name = $wpdb->prefix . "plus8k_media";
$sql = "DROP TABLE $table_name";
dbDelta( $sql );
}
If I for instance remove functions and just copy the code from plus8k_activate outside function, the tables get created. I don't know why the registered hook doesn't fire the plus8k_activate function.
EDIT! Activate didn't work because error in sql (boolean default must be 1 or 0, it doesnt recognize true/false)
Now activate work, but deactivate doesn't although sqls are ok, I've tryed it with phpmyadmin. Need to figure that out.
Ensure that you call this function from the main file of your plugin (like sample-plugin/sample.php).
Alternatively try the anonymous function callback to ensure that you are not making any mispelings etc.
register_deactivation_hook(__FILE__, function () {
//something here like
die('today');
});
When you deactivate your plugin this code has to run. Please take it from there adding your functions inside.
Your code looks okay to me.
Make sure your filename is like your plugins name. E.g. if your plugin is called myplugin, the activation script needs to be in wp-content/plugins/myplugin/myplugin.php
To remove the table, you can't use dbDelta() - this will only create the table or modify it, but not remove it.
To drop a table, you need a regular query:
$table_name = $wpdb->prefix . "plus8k_products";
$sql = "DROP TABLE $table_name";
$wpdb->query($sql);

Wordpress plugin activation creating table

I am trying to create a table with my wordpress plugin on activation. I have this and it is not working:
function activate_cron() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "fbcron";
$sql = "CREATE TABLE ".$table_name." (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
) ".$charset_collate.";";
require_once( '../../../wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
register_activation_hook( __FILE__, 'activate_cron' );
I believe for dbDelta the collate needs to be on its own line. It can be easier to make sure you have the right spacing by embedding the variables in the string:
$sql = "CREATE TABLE {$table_name} (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
)
{$charset_collate};";
I think you have a problem with wp-admin/includes/upgrade.php path
I improve your existing code, tested
function activate_cron() {
global $wpdb;
$table_name = $wpdb->prefix . 'fbcron';
//check if table exist
if($wpdb->get_var("SHOW TABLES LIKE '$table_name';") != $table_name) {
$sql = "CREATE TABLE ".$table_name." (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
) ".$charset_collate.";";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); //this path
dbDelta($sql);
}
}
register_activation_hook( __FILE__, 'activate_cron' );
Hope it helps

how to create a table inside plugin in wordpress database

I want to create a table in existing wordpress database.and i am unable to create it . I have write this code inside a custom plugin files.Please help me through this code.
<?php
/**
* #package Demo
*/
/*
Plugin Name: Demo
Plugin URI: https://demo.com/
Description: This is used for the plugin database.
Version: 3.2
Author: Automattic
Author URI: https://automattic.com/wordpress-plugins/
License: GPLv2 or later
Text Domain: demo
*/
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'your_plugin_options_install');
// function to create the DB / Options / Defaults
function your_plugin_options_install() {
global $wpdb;
global $db_version;
$your_db_name = $wpdb->prefix . 'db_record';
// create the ECPT metabox database table
$sql = "CREATE TABLE " . $your_db_name . " (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`field_1` mediumtext NOT NULL,
`field_2` tinytext NOT NULL,
`field_3` tinytext NOT NULL,
`field_4` tinytext NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
add_option("db_version", $db_version);
}
?>
function wpa_install() {
/*Create Table on Plugin Activation*/
global $wpdb;
$table_name = $wpdb->prefix . "table_name";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
`id` int(11) NOT NULL AUTO_INCREMENT,
`records` text COLLATE utf8mb4_unicode_ci NOT NULL,
`loggedin_time` datetime NOT NULL,
`unique_number` varchar(8) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) $charset_collate";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
/*Create Table on Plugin Activation*/
}
class WP_DiscoverU {
// Constructor
function __construct()
register_deactivation_hook(__FILE__, array($this, 'wpa_uninstall'));
//call function when activate plugin
}
}
Above code for generate table when activate plugin.

Can't create MYSQL table for Wordpress plugin

I'm trying to create a table in my database to store my Wordpress plugin settings. I'm using the code below, but it doesn't work. I don't get any errors but the table is not created, and it seems that the function is not executed at all.
What am I doing wrong?
function ADR_create()
{
global $wpdb;
$table_name = $wpdb->prefix. "Author_detailed_repport";
global $charset_collate;
$charset_collate = $wpdb->get_charset_collate();
global $db_version;
if( $wpdb->get_var("SHOW TABLES LIKE ".$table_name ) != $table_name)
{ $create_sql = "CREATE TABLE " . $table_name . "(
ADR_id INT(11) NOT NULL auto_increment,
role VARCHAR(30) NOT NULL,
statut INT(1) NOT NULL,
post_number INT(4) NOT NULL,
activate INT(1) NOT NULL,
UNIQUE (ADR_id) )$charset_collate;";
}
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta( $create_sql );
if (!isset($wpdb->Author_detailed_repport))
{
$wpdb->Author_detailed_repport = $table_name;
//add the shortcut so you can use $wpdb->stats
$wpdb->tables[] = str_replace($wpdb->prefix, '', $table_name);
}
}
register_activation_hook(__FILE__, 'ADR_create');
i have fix the problem, there is nothing wrong with this code, i was just typing it in the wrong file

Wordpress not creating database table on activitation

$table_name = $wpdb->prefix . 'offline_card';
// function to create the DB / Options / Defaults
function offline_card_install() {
global $wpdb;
global $table_name;
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$table_name'") != $table_name)
{
$sql = "CREATE TABLE $table_name (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`order_id` int NOT NULL,
`card_number` varchar(55) NOT NULL,
`card_expiry` varchar(55) NOT NULL,
`card_ccv` varchar(22) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'offline_card_install');
I don't understand what is the reason that it is not creating database table on plugin activation. It is not showing any error though.
Your code looks okay. Might be issue with your variable $table_name.Write it into function and try once:
function offline_card_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'offline_card';
Have you checked $table_name variable returns anything?

Categories