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
Related
I wrote a plugin that included another plugin, but nothing happens when I run it. Please check this out and help me.
This is the first plugin, and this is the main plugin👇
<?php
/*
Plugin Name: DB Testsed
Description: a simple plugin
Version: 1.0
Author: Ak
Text Domain: HOooooooo
// */
include "ADT_db.php";
?>
This is the second plugin, which I added in the main plugin 👇
<?php
function scratchcode_create_payment_table()
{
global $wpdb;
$table_name = $wpdb->prefix . "ADT";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) UNSIGNED NOT NULL,
created_at datetime NOT NULL,
expires_at datetime NOT NULL,
PRIMARY KEY id (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
addMenu();
}
register_activation_hook(__FILE__,'scratchcode_create_payment_table');
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 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 very new to WordPress, and writing a plugin to (ideally) allow our client to update their existing WP site and the dynamic info on our iphone app all in one place. This will require the creation of two tables. I have written what I believe should be creating these tables, but activating the plugin throws this error on my test WordPress site:
The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
Looking into the database also reveals that the tables were not created. I have looked at a handful of other posts here and elsewhere, but can't seem to isolate where I am going wrong. Any suggestions?:
<?php
// Plugin info omitted
register_activation_hook(_FILE_, 'event_manager_install');
register_deactivation_hook(_FILE_, 'event_manager_uninstall');
// Installer
function event_manager_install() {
global $wpdb;
// Builds queries for custom event and speakers tables
$event_table = $wpdb->prefix . 'events';
$eventSql = "CREATE TABLE $event_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
venueName VARCHAR(250) DEFAULT '' NOT NULL,
date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
address text NOT NULL,
registrationDeadlineDate datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
UNIQUE KEY id (id)
);";
$speaker_table = $wpdb->prefix . 'speakers';
$speakerSql = "CREATE TABLE $speaker_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
eventID mediumint(9) NOT NULL,
speaker text NOT NULL,
UNIQUE KEY id (id)
);";
// Executes queries
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($eventSql);
dbDelta($speakerSql);
}
// Uninstaller
function event_manager_uninstall() {
global $wpdb;
// Creates queries to delete custom tables
$event_table = $wpdb->prefix . 'events';
$speaker_table = $wpdb->prefix . 'speakers';
$eventSql = "DROP TABLE " . $event_table . ";";
$speakerSql = "DROP TABLE " . $speaker_table . ";";
// Executes deletion queries
$wpdb->query($eventSql);
$wpdb->query($speakerSql);
}
?>
The problem is that you've misspelled the name of the __FILE__ constant. Just replace _FILE_ with __FILE__ (note: two underscores), and it should work just fine.