WORDPRESS UPDATE DB method not get called - php

I am making a wordpress plugin, where I have functions in the main php file:
<?php
include_once "define.php";
include_once 'includes/form_processor.php';
include_once 'includes/email_subscriber.php';
include_once 'includes/options_page.php';
include_once 'includes/widget.php';
//register the plugin installation methods
register_activation_hook(__FILE__, 'install_simple_subscriber_plugin');
register_deactivation_hook(__FILE__, 'uninstall_simple_subscriber_plugin');
//register the plugin url
wp_enqueue_style('simple-email-subscriber-css', plugins_url('/simple_email_subscriber/stylesheets/style.css'));
function install_simple_subscriber_plugin(){
add_action('plugins_loaded', 'setup_plugin_actions');
email_subscriber::install_database();
email_subscriber::update_database();
}
function setup_plugin_actions(){
if(has_action('publish_post')){
$simple_email_subscriber = new email_subscriber();
//add action listener to post publication
add_action('publish_post', array($simple_email_subscriber, 'on_publish_post'));
add_action('publish__future_post', array($simple_email_subscriber, 'on_publish_post'));
}
//add the admin menu pages
$options_page = new options_page();
}
function uninstall_simple_subscriber_plugin(){
remove_action('plugins_loaded', 'setup_plugin_actions');
}
?>
AND in my custom class I have:
<?php
class email_subscriber{
/*
* Plugin Core Methods
* */
static function install_database() {
//define create table query
$sql = "CREATE TABLE IF NOT EXISTS ".SIMPLE_EMAIL_SUBSCRIBER_DB_NAME."(
id mediumint(9) NOT NULL AUTO_INCREMENT,
subscribe_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
email VARCHAR(55) NOT NULL,
UNIQUE KEY id (id)
);";
//update db and make it auto adjust
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
//HERE IS THE NEW BITS ADDED TO THE EXISTING PLUGIN THAT'S NOT GET CALLED
static function update_database(){
$sql=
"DELIMITER $$
CREATE PROCEDURE Alter_Table()
BEGIN
DECLARE _count INT;
SET _count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'email_subscription' AND
COLUMN_NAME = 'subscribe_all');
IF _count = 0 THEN
ALTER TABLE ".SIMPLE_EMAIL_SUBSCRIBER_DB_NAME."
ADD COLUMN subscribe_all TINYINT(1) DEFAULT 1,
ADD COLUMN subscribe_category varchar(512) DEFAULT NULL;
END IF;
END $$
DELIMITER ;
CALL Alter_Table();
DROP PROCEDURE Alter_Table;
";
//update db and make it auto adjust
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
//add the options for db defination
add_option("ses_db_version", SES_PLUGIN_VERSION);
}
.................and other codes
}
The update_database() method is the new method I added in, that's trying to update the database for extra functions. but it's never get run when the plugin activates.

You just want to execute that query.
global $wpdb;
$wpdb->query($sql);
dbDelta is used to create new tables or update existing ones to conform to an updated scheme. Simply put, it accepts a CREATE TABLE query and transforms it into an ALTER TABLE query if necessary. The Codex says:
The dbDelta function examines the current table structure, compares it
to the desired table structure, and either adds or modifies the table
as necessary, so it can be very handy for updates [...] Note that the
dbDelta function is rather picky, however.

Related

database not creating and giving error on plugin activation

I am new to development so any feedback is appreciated - i have built a wordpress plugin that needs to create a new database table on plugin activation. When I activate the plugin i get the following error, and the new table has not been created.
'The plugin generated 1050 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.'
Here's the code:
global $ed_wp_cardealer_db_version;
$ed_wp_cardealer_db_version = '1.0';
//create database table
function create_database(){
global $wpdb;
global $ed_wp_cardealer_db_version;
$table_name = $wpdb->prefix . 'ed_wp_cardealer';
$charset = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
valuation_id int NOT NULL AUTO_INCREMENT,
vrm TINYTEXT,
price int(9),
vehicleDescription TINYTEXT,
email TINYTEXT,
PRIMARY KEY (valuation_id)
)$charset;";
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta( $sql );
add_option( 'ed_wp_cardealer_db_version', $ed_wp_cardealer_db_version );
}
//create database on plugin activation
register_activation_hook( __FILE__, 'create_database' );
I've checked it over and over against the wordpress official docs and can't see any obvious errors. If I activate the plugin without this line then there is no error, but the database is still not created
register_activation_hook( __FILE__, 'create_database' );
I have seen lot's of responses to similar queries that suggest removing extra space from opening php tag etc, but I need to create the new table as well as remove the error.
I have set debug to true in wp-config, but can't see any further details than i have given.
I change valuation_id int value length. When you declare int value in DB must assign int length. Also, add two extra parameters on add_option.
Please try the below code hopefully work it. I test it on my local development server.
global $ed_wp_cardealer_db_version;
$ed_wp_cardealer_db_version = '1.0';
//create database table
function create_database(){
global $wpdb;
global $ed_wp_cardealer_db_version;
$table_name = $wpdb->prefix . 'ed_wp_cardealer';
$charset = $wpdb->get_charset_collate();
$sql = " CREATE TABLE $table_name (
valuation_id int(11) NOT NULL AUTO_INCREMENT,
vrm TINYTEXT,
price int(9),
vehicleDescription TINYTEXT,
email TINYTEXT,
PRIMARY KEY ( valuation_id )
)$charset;";
require_once( ABSPATH . "wp-admin/includes/upgrade.php" );
dbDelta( $sql );
add_option( 'ed_wp_cardealer_db_version', $ed_wp_cardealer_db_version, '', 'yes' );
}
//create database on plugin activation
register_activation_hook( __FILE__, 'create_database' );
Screenshot:

Not create trigger using dbDelta() in wordpress

I am trying to CREATE TRIGGER on custom table in mySQL db with dbDelta() function in wordpress plugin I am building!
On plugin activation, tables are created and populated with default data. When execute this function:
function PDFsem_initial_trigger($ldd_tabela) {
global $wpdb;
$table_name = $wpdb->prefix.$ldd_tabela;
//it works!!!
$sql = "CREATE TRIGGER brisIspod BEFORE DELETE ON ".$table_name." FOR EACH ROW INSERT INTO wp_ldd_usluga (usluga_naziv,usluga_rbr,seminar_id) VALUES('NEW TRIGGERED DATA', 2, 2)";
/*
//dont work!!!
$sql = "CREATE TRIGGER brisIspod BEFORE DELETE ON wp_ldd_seminar FOR EACH ROW DELETE FROM wp_ldd_usluga WHERE wp_ldd_usluga.seminar_id = old.id_seminar;";
*/
dbDelta( $sql );
}
Function creates trigger on BEFORE DELETE ... INSERT INTO..., but wont create trigger on BEFORE DELETE ... DELETE... . If execute sql query in phpMysqlAdmin console directly trigger is created regularly.
I am not creating both triggers in same time so one is under comment. First trigger is just test sample.
Is it possible to add trigger I want with dbDelta() in wordpress mySql db?
Trigger in mySql can be created with :
$sql = "CREATE TRIGGER brisIspod BEFORE DELETE ON wp_ldd_seminar FOR EACH ROW DELETE FROM wp_ldd_usluga WHERE wp_ldd_usluga.seminar_id = old.id_seminar;";
$wpdb->query($sql);

Writing a wordpress plugin, trying to retrieve data from customized table?

I am trying to create a wordpress plugin. I had created a table and trying to retrieve data from the table but it give me an error message
Warning: Missing argument 2 for wpdb::prepare(), called in
/home2/l2on708/public_html/mysite.com/wp-content/plugins/myplugin/myplugin.php
on line 79 and defined in
/home2/l2on708/public_html/mysite.com/wp-includes/wp-db.php on line
1210
<?php
$table_name = $wpdb->prefix . "gallery_rating";
function options_page(){
/*
* displaying back-end plugin page
*/
global $table_name;
global $wpdb;
$stmt = $wpdb->prepare("SELECT * FROM $table_name ORDER BY id DESC LIMIT 1 ");
$stmt->execute();
$rows = $stm->fetchALL(PDO::FETCH_ASSOC);
foreach ($rows as $rows) {
$options_link = $rows['link'];
}
}
function table_install () {
global $wpdb;
global $table_name;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
link varchar(100) NOT NULL,
src varchar(250) NOT NULL,
click int(11) NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'table_install');
?>
As of WordPress 3.5, wpdb::prepare() enforces a minimum of 2 arguments (as the error tells you):
The query
A value to substitute into the placeholder.
More info can be found in the Codex.
That said, prepare() may not even be the correct method for you to be using in this case; you may be better off using something like get_results().

Wordpress create table by plugin

How create table (if not exists) by activation plugin?
Note: plugin works fine if table exists, but i need create table if not exists
what i try:
function alicelf_bookmark() {
global $wpdb;
$table_name = $wpdb->prefix . "alice_user_bookmarks";
$charset_collate = '';
if ( ! empty( $wpdb->charset ) )
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
if ( ! empty( $wpdb->collate ) )
$charset_collate .= " COLLATE {$wpdb->collate}";
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT NOT NULL AUTO_INCREMENT,
user_id INT,
name VARCHAR(255),
shipping_address VARCHAR(255),
user_notes TEXT,
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
//some other plugin stuff..
}
add_action('wp_footer', 'alicelf_bookmark');
Maybe im use wrong action?
When your plugin requires it's own table, you will want to take into account that this table can change over time. You know it will change (from nothing to your first version) on first install. Later it can change when a user updates the plugin.
A great way to do this is to store a database version number in the options table, and check if it's updated. If the version number in the options table doesn't match the one in your plugin, an install is needed.
This brings us to how your plugin should handle changes in the table structure. A simple approach could include a whole lot of if-tests and custom ALTER TABLE queries (the same queries aren't necesserily needed when upgrading from 1.0 to 3.0 as the ones you would need to upgrade from 1.0 to 3.0). This could become hard to work with pretty fast.
It is generally better to leave the ALTER TABLE statements to WordPress' own dbDelta function, which will generate the statements you need.
<?php
$prefix_table_name = $wpdb->prefix . 'prefix_table_name';
$prefix_db_version = '1.0';
$prefix_db_installed_version = get_option( 'prefix_db_version' );
/**
* Create or update tables if needed.
*/
function prefix_install_db() {
global $wpdb;
if ( $prefix_db_installed_version == $prefix_db_version )
return; // Database is current version, no need to do anything
// We'll need dbDelta()
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
$sql = "CREATE TABLE " . $prefix_table_name . " (
ID bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
email varchar(320) NOT NULL,
PRIMARY KEY ID(ID)
);";
dbDelta( $sql );
update_option( 'prefix_db_version' , $prefix_db_version );
}
add_action( 'plugins_loaded', 'prefix_install_db' );
For further reading, a great guide to Creating Tables with Plugins can be found in the Codex.
Found solution: Just need register activate and change wp_head instead wp_footer
add_action('wp_head', 'alicelf_bookmark');
//also move away your activation and sql create table to another func for escape some errors
//when you enable plugin and table exists
function activator_bookmark(){
//do code
}
register_activation_hook(FILE,'activator_bookmark');

Add new column to wordpress database

I trying update my plugin. So I must upgrade mysql_table. But when trying new column, program get exception.
This is my current table :
$sql = "CREATE TABLE {$table_name} (
say_id int(11) not null AUTO_INCREMENT,
customer_mail text not null,
customer_name text not null,
customer_messagge text not null,
messagge_date_time datetime not null,
PRIMARY KEY (say_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1";
require_once(ABSPATH . "wp-admin/includes/upgrade.php");
dbDelta($sql);
Now I am add colum more one table. I try Alter table, this working one time and add a column but one more refresh I get this error.
This is mycode
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
And this is my error
WordPress database error: [Duplicate column name 'say_state']
ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1
I see this error and ı try this;
$query = $wpdb->query("select * from wp_customer_say");
$respond = mysql_num_fields( $query );
$column_array = array();
for($i = 0; $i < $respond ; $i++):
$column_array[] = mysql_field_name($query,$i);
endfor;
if( !in_array("say_state",$column_array) ):
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
endif;
and I get this error.
Warning: mysql_num_fields() expects parameter 1 to be resource, integer given in
Help please. Thank you.
Sorry bad english.
Use this query. I use only mysql-standred for get field name by fast query and this will solve your problem:
$row = $wpdb->get_results( "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'wp_customer_say' AND column_name = 'say_state'" );
if(empty($row)){
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
}
You can check column name exists in WordPress using below way,
$myCustomer = $wpdb->get_row("SELECT * FROM wp_customer_say");
//Add column if not present.
if(!isset($myCustomer->say_state)){
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT 1");
}
Best and correct way to add new column into the table if column not exists.
/*# Add status column if not exist */
global $wpdb;
$dbname = $wpdb->dbname;
$marks_table_name = $wpdb->prefix . "wpsp_mark";
$is_status_col = $wpdb->get_results( "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `table_name` = '{$marks_table_name}' AND `TABLE_SCHEMA` = '{$dbname}' AND `COLUMN_NAME` = 'status'" );
if( empty($is_status_col) ):
$add_status_column = "ALTER TABLE `{$marks_table_name}` ADD `status` VARCHAR(50) NULL DEFAULT NULL AFTER `attendance`; ";
$wpdb->query( $add_status_column );
endif;
Just wanted to add that there's a slightly better way to do this than #Amandeep Wadhawan's answer.
register_activation_hook(__FILE__, 'install_tables');
function install_tables()
{
update_options('my_plugins_current_db_version', 0); // Replace 0 with whatever your final database version is
// Code here to create the final version of your database tables
}
add_action('plugins_loaded', 'update_databases');
public function update_databases()
{
global $wpdb;
$prefix = $wpdb->prefix;
$a_table_to_update = $prefix . $a_table_to_update;
$current_version = get_option('my_plugins_current_db_version', 0);
switch($current_version)
{
// First update
case 0:
// first update code goes here (alter, new table, whatever)
$current_version++;
case 1:
// next update code goes here
$current_version++;
}
update_option('my_plugins_current_db_version', $current_version);
}
You just have to make sure that your install_tables() function will always create the tables that reflect the final version number and sets the my_plugins_current_db_version option to the final version number.
Then in the update_databases() function you just have to make sure to increment $current_version before each subsequent case.
With this set-up you can blindly update without having to unnecessarily query to find out if columns or tables exist first - and less queries are always a good thing....especially if your update code is firing on the plugins_loaded hook.
It is also much cleaner, shows a clear upgrade path, and only executes necessary updates - so it is more efficient.
I really like Rikesh's option (even upvoted!), but I think to prevent hardcoding information which could change, such as $table_prefix from the wp-config.php file, this option is a safer bet.
// Replace `table_name` with the actual table name
$table = $table_prefix.'table_name';
// And use sprintf to pass the table name
// Alternatively, you can use $table instead of sprintf,
// if you use double quotes such as shown here
$myCustomer = $wpdb->get_row( sprintf("SELECT * FROM %s LIMIT 1", $table) );
// If you prefer not using sprintf, make sure you use double quotes
// $myCustomer = $wpdb->get_row( "SELECT * FROM $table LIMIT 1" );
// Replace "missing_field" by the column you wish to add
if(!isset($myCustomer->missing_field)) {
$wpdb->query( sprintf( "ALTER TABLE %s ADD phone2 VARCHAR(255) NOT NULL", $table) );
// Alternate example using variable
// $wpdb->query( "ALTER TABLE $table ADD phone2 VARCHAR(255) NOT NULL") );
}
Wrote a good solution, it works in all cases
$check_column = (array) $wpdb->get_results( "SELECT count(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME = '{$wpdb->prefix}my_table' AND COLUMN_NAME = 'some_name'" )[0];
$table_name = $wpdb->prefix . 'my_table';
$check_column = (int) array_shift($check_column);
if($check_column == 0) {
$wpdb->query(
"ALTER TABLE $table_name
ADD COLUMN `some_name` VARCHAR(55) NOT NULL
");
}
$myCustomer = $wpdb->get_row("SELECT * FROM wp_customer_say");
//Add column if not present.
if(!isset($myCustomer->say_state)){
$wpdb->query("ALTER TABLE wp_customer_say ADD say_state INT(1) NOT NULL DEFAULT '' ");
This code it will fail if you have no records in the database. Only works if you have at least one record saved in your database.

Categories