Using custom database tables for your WordPress plugins is one of the way that you can greatly improve the kind of plugins you are able to create. DB tables give you a way to manage, organize, and save data for your plugin, and creating them for your plugin is very simple.
This is a quick tip. Check out more Quick Tips
The following code can be placed in your main plugin file. The function, which will create the necessary database tables only if they don’t already exist, will be run when the plugin is activated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | $your_db_name = $wpdb->prefix . 'your_db_name'; // function to create the DB / Options / Defaults function your_plugin_options_install() { global $wpdb; global $your_db_name; // create the ECPT metabox database table if($wpdb->get_var("show tables like '$your_db_name'") != $your_db_name) { $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); } } // run the install scripts upon plugin activation register_activation_hook(__FILE__,'your_plugin_options_install'); |

Any tips on debugging this? I cannot tell you how many times I’ve tried adding a table in this manner only to get the dreaded “You have X character of unexpected output…” The best way I’ve found to debug this was to call var_dump in the core (cannot remember where at the moment). Wondering if you have any tips to make this go better.
@Zach – I don’t have any particular tips. Using var_dump does work well. If you do it the way I’ve described here, you shouldn’t get the “X char” error.
Absolutely…I use the same technique, but since dbDelta is so picky you some times get weird errors. My favorite is when the SQL executes just fine when you directly query it, but running it through dbDelta chokes.
[...] here: Create Database Tables When Plugin is Activated | Pippin’s Plugins Posted in WordPress Plugins « 40 Free And Premium WP Themes Ideal For Portfolios 10 Best [...]
What about deleting tables when your plugin is removed?
Take a look at the uninstall hook: http://codex.wordpress.org/Function_Reference/register_uninstall_hook
Ok wpdb->insert is not working for me at all.
I’m using it through the ajax so can that be related to my problem. Every other thing i saw on the internet doesn’t help me at all, and all the things are correct checked everything milion times.
Ok let me know if you can help me
thanks for that, some way it helps, but still didn’t cover my question about this
-I’m trying to connect my DB to my code which I have it already in plugin, to execute a simple PHP code for viewing data form my DB such as name, description, price and the most important “IMAGES”
-I followed your tutorial about “writing you first plugin…” and I got it very well, put I have a question ,, can I replace “single OR singular” by something means like that … if post (or page) = to NAME_OF_POST3 ,, any name that we have created it using dashboard
I’m not sure I understand your question, sorry. Can you rephrase it?
okay
I know about writing plugin,, I wrote one to save a twitter url from the setting which I write it, to show the Twitter info.after the content of pages and posts.. (I was followed you tutorial)
Now I want to write a plugin to be show in the site page ,, which contain photos (as table of photos) and brief info. About each photo, and then user can click on it and see more detail about it , then add to cart it if they want to buy (that item) …. then go to the payment page and pay ,,,
hint: the admin is the one who is gonna add the items info such as(upload photo, discription and price and name ) then it gonna show in the menu…..
I’ve done this before … but in regular a theme, how can I use my codes and do some changes to them to be active in WordPress theme
my fingers confused a little bit in typing,, I am sorry about that
I want to write a plugin to be shown in the site page for example if I have a page called E-menu ,, how can I determine this thing cause I don’t want it to be in all pages, or all post, by using sing/singular…
There’s no difference in writing this as a plugin than there is in writing it as a theme. The code is identical, just in a different location.
you mean that, I can to create my pages inside the “theme folder” which I have it in “wp-content”
And I can determine where I would like to show that page?
such as: sing up, menu item for sell, register page, payment page,, ?
a quick question also, how can I add “log in” above the header,, I know I have to “log in” link automatically down in within the footer,, how can I change that
(I hope to find answers from you) cause I spent weeks to figure out the answers.. andI have no time to finish my task… please help >.<"