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
Related
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 );
}
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.
function create_xml_table(){
global $wpdb;
$tableName = $wpdb->prefix."XMLdata";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
user_id varchar(255) NOT NULL,
post_id varchar(255) NOT NULL,
value varchar(255) DEFAULT '' NOT NULL,
time timestamp ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
register_activation_hook(__FILE__, 'create_xml_table');
}
You have to do like this.
function create_xml_table(){
global $wpdb;
$tableName = $wpdb->prefix."XMLdata";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
user_id varchar(255) NOT NULL,
post_id varchar(255) NOT NULL,
value varchar(255) DEFAULT '' NOT NULL,
time timestamp ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)$charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
register_activation_hook(__FILE__, 'create_xml_table');
i am begineer in wordpress , i want to create table when we activate a plugin .'
i have written a code for the same but it is not working . can any one find out what is wrong with this code . so that it will create table successfully
<?php
/*
Plugin Name: dbtable
*/
$tbl = $wpdb->prefix . 'member';
function myreg_func(){
global $wpdb;
global $tbl;
$sql="CREATE TABLE IF NOT EXISTS $tbl (id int not null AUTO_INCREMENT,
em varchar(255) NOT NULL,
mob BIGING NOT NULL ,
nm varchar (225) NOT NULL ,
PRIMARY KEY(id))";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
register_activation_hook(__FILE__,'myreg_func');
?>
<?php
/*
Plugin Name: check
*/
function plugin_name_activation() {
require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
global $wpdb;
$db_table_name = $wpdb->prefix . 'member';
$sql = "CREATE TABLE " . $db_table_name . " (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`em` varchar(100) NOT NULL ,
`mob` int(20) NOT NULL,
`nm` varchar (225) NOT NULL ,
PRIMARY KEY (`id`)
) ;";
dbDelta( $sql );
}
register_activation_hook(__FILE__, 'plugin_name_activation');
?>
I have been trying to create a new table for my wordpress plugin for a few hours now without success.
My best guess is that there is some error in the sql which i cannot find. I have been trying to look at the wordpress codex and other sources as well without success. Would apprechiate some help!
register_activation_hook( __FILE__, 'plugin_install' );
function plugin_install(){
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;
$table_name = $wpdb->prefix . 'dbname';
$charset_collate = $wpdb->get_charset_collate();
$sql_create_table = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL PRIMARY KEY AUTO_INCREMENT,
b_id int(11) NOT NULL,
iprefix varchar(20) NOT NULL default '0',
istart bigint(20) NOT NULL default 'updated',
iend bigint(20) unsigned NOT NULL default '0',
isuffix varchar(20) NOT NULL default 'post',
PRIMARY KEY (id)
) $charset_collate; ";
dbDelta( $sql_create_table );
}
In the fourth line of your create SQL code you have an unsigned varchar. Remove the unsigned keyword and it should be fine.
$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?