Due to some changes in the DB of my plugins, I need to alter a table to add some columns to it, but even though the function is running, the table isn’t altered. Here’s the function that I’ve written
$sql = "ALTER TABLE `wp_tnt_videos` ADD `date_created` INT NOT NULL DEFAULT '0' AFTER `video_order`, ADD `date_modified` INT NOT NULL DEFAULT '0' AFTER `date_created`, ADD `user_id` INT NOT NULL DEFAULT '0' AFTER `date_modified`;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
I read this: http://hungred.com/how-to/wordpress-dbdelta-function/
And tried to check sql command carefully. I also run that sql command in mysql and it work!!
So, please help me! 😐
Your answer is in the Codex:
For one, you’ve got backticks around field names. That rule probably applies to table names too but I am not sure.
Additionally, you are trying to use
dbDelta
as if you were making a direct query to the database. It doesn’t work that way. You need to give it the complete desired table structure usingCREATE TABLE
as if you are creating new every time and …You don’t give it
ALTER TABLE
commands or feed it just the parts you want to change. It is not a general purpose database alteration tool in that way. And, as already mentioned, it is very picky. Format your SQL exactly like in the examples, line breaks included.I would add that you need to watch this function very carefully. It fails silently and seems to not what to do certain things, like delete indexes– at least I have had a lot of trouble with that. I am not sure if it will move columns around to insert new ones or just add to the end.
You just have to execute your SQL command like that :