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');
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 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.
<?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.
$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