WordPress table creation throwing errors (and not creating tables) - php

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.

Related

nothing happens when I run plugin

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');

sql creates two tables using $wpdb->prefix

$tablename = $wpdb->prefix . "name";
$sql = "CREATE TABLE IF NOT EXISTS $tablename(
time_id INT(11) NOT NULL AUTO_INCREMENT,
job VARCHAR(120) NOT NULL,
employee VARCHAR(100) NOT NULL,
date_entry DATETIME NOT NULL,
duration INT(11) NOT NULL,
PRIMARY KEY(time_id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
creates two tables: 'wp_name' and 'name'. If I define $tablename = 'wp_name' instead it only creates one table. Also '{$wpdb->prefix}name' doesn't change the problem.
What's the problem here? - thanks!
Get rd of the brackets
$sql = "CREATE TABLE IF NOT EXISTS $tablename (
...
);";
They are only used to replace internal wp variables
Update:
your case can't create two tables by itself, there is no way, you have somewhere the same code twice.
Without the prefix, the second code checks that there is already a table named nameand does nothing, but when you add the prefix, it tries to add the tbale name which azt this time doesn't exist, because it is call wp_name

my table is not getting created in wordpress plugin

I am new to wordpress , I am creating the table but I dont know where is the error occuring , hence table is not creating in database , my table code is below
$table_name = $wpdb->prefix . "myuser";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
`id` int(11) AUTO_INCREAMENT NOT NULL ,
`name` varchar(50) CHARACTER SET utf8 NOT NULL,
`email` varchar(50) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) $charset_collate; ";
and I am inserting data in it but there is no error showing neither the table is creating
$table_name = $wpdb->prefix . "myuser";
$wpdb->insert(
$table_name, //table
array('name' => $name, 'email' => $email), //data
array('%s', '%s') //data format
);
$message.="Users added successfully";
You've only defined the sql query yet, you still need to execute it.
According to this article you can use the following code:
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
Just put it right after the part that starts with $sql =.
Assuming you're writing some kind of plugin, notice you'll also have to add the function as register_activation_hook.
For more information, see Writing a plugin and Creating Tables with Plugins.

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);

My custom table not being created on wordpress plugin activation

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

Categories