Having an issue with dbDelta to add table to wp db (resolved)

I’m having some issues with trying to put the WP-Twitip-ID plugin into the final version.

Right now, the beta version stores the twitter ID in the post meta for each post but that’s not really ideal because it can only show the twitter id on the comments of the post that the user has already commented on. New post comments require the user to add their twitter id again. Not too much trouble because if they have entered a twitter id in a previous comment then the plugin sets a cookie on their machine and adds it to the field when they comment again.

What I’d rather do is store the twitter id in the database so it’s accessible outside the post meta. I could store the id’s in wp_options but that would probably cause loading time issues because wp_options values get loaded automatically and considering the amount of comments a blog gets, it would bloat it out massively.

My solution is to create a new table on the database and add the twitter id and email address in there, that way I only have to update the db once when a new twitter id is entered.

The problem I am having is creating the table! for some reason, it’s not adding it. I’ve even copied and pasted another plugins code to do it and it still wont work!

here’s my code :

function atf_install() {
global $wpdb;
global $atf_db_version;
add_option("atf_db_version", $atf_db_version);

$table_name = $wpdb->prefix . "wptwitipid";
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
email varchar(120) NOT NULL,
twitid varchar(120) NOT NULL,
KEY email (email)
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$atf_db_version = "1.0";
register_activation_hook(__FILE__, 'atf_install');

There’s a problem with the $atf_db_version too. It adds an option to wp_options for the key of “atf_db_version” but no value and it’s really starting to piss me off!

anyone out there in geek land that can help me here?


A massive thanks to @clearskynet who answered and solved the problem within minutes of me posting this.


8 thoughts on “Having an issue with dbDelta to add table to wp db (resolved)

  1. AnttiJN

    The reason why you dont get the value to wp_options can be found here:

    It says “If you’re using global variables, you may find that the function you pass to register_activation_hook() does not have access to global variables at the point when it is called, even though you state their global scope within the function”.

    So if you define $atf_db_version inside the function it should work.

  2. Petra Weiss

    This seems like the right blog to post my question, with all the WordPress experts here!
    I know this probably doesn’t belong in this thread, but I will try to get some help anyway.
    On my blog http://www.healthydietweightloss.com something very strange has happened. I tried to upload the sociable plugin through WP 2.7’s new direct plugin uploader, and got an error message. It didn’t work. But ever since on my home page the writing has gone to italic. Everything but my posts which I did since trying to upload the plugin. If you click through on any of the posts, it’s fine, it seems to be just the index page. Can anybody offer help? – I looked at the theme files, nothing seems to have changed there. I know also that when you view source there seem to be a lot of em tags there, but they are not visible in the theme files. I am out of my depth here. :(

    Petra Weiss´s last blog post..Free Affiliate Insider – Update