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
Related
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);
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
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 );
}
$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?
I am trying to create my own custom table in wordpress database on plugin activation ..This is my code for that ..
function __construct()
{
register_activation_hook(__FILE__,array(&$this, 'activate'));
function activate()
{
global $wpdb;
echo "<div class='updated'>Test Plugin Notice</div>";
$table_name = $wpdb->prefix . "dive";
$installed_ver = get_option( "divebook_db_table_dive_version" );
//Check if the table already exists and if the table is up to date, if not create it
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name
|| $installed_ver != $divebook_db_table_dive_version ) {
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
date bigint(11) DEFAULT '0' NOT NULL,
site tinytext NOT NULL,
description text NOT NULL,
max_depth mediumint(9) NOT NULL,
time mediumint(9) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option( "divebook_db_table_dive_version", $divebook_db_table_dive_version );
}
//Add database table versions to options
add_option("divebook_db_table_dive_version", $divebook_db_table_dive_version);
}
}
I m checking my database .No new table has been created ..Plz look into it ..
That's because any WP activation/deactivation hook needs to be run from inside the plugin main file, not from a file you include in the main file.
So, try to run the activation hook from your plugin-name.php file and it will work.
L.E:
also, i don't see $divebook_db_table_dive_version being defined. Another thing, dbDelta caused problems for me in the past, try with $wpdb->query() to run the create table query.
It may be because of incorrect SQL query
just check the documentation here
https://codex.wordpress.org/Creating_Tables_with_Plugins