Hi I've written a function to add a table. I've tested my syntax and it works in my phpmyadmin but for some reason it's not being activated in my plugin? :S
class clients{
public static $version="1.0";
protected static function setup_tables(){
global$wpdb;
require_once(ABSPATH."/wp-admin/includes/upgrades.php");
dbDelta("CREATE TABLE ".$wpdb->prefix."hippie_clients (ID mediumint(9) NOT NULL AUTO_INCREMENT, email varchar(100) NOT NULL, password varchar(512), paid tinyint(1) NOT NULL DEFAULT 0, firstname varchar(50) NOT NULL, lastname varchar(70) NOT NULL,signup TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, free tinyint(1) DEFAULT 1,gender tinyint(1) NOT NULL, lastlog varchar(200), UNIQUE(email), PRIMARY KEY(ID))$wpdb->get_charset_collate();");
if(!isset($wpdb->clients))$wpdb->clients=$wpdb->prefix."hippie_clients";
add_option('Hippie_clients_version',self::$version);
}
//I'm using another class to manage my activation hooks etc but this is the basic idea
public static function setup(){
self::setup_tables();
}
public function run(){
register_activation_hook(__FILE__,(array('clients','setup'));
}
}
$clients=new clients();
$clients->run();
But the table isn't being added. Is my table syntax? Or the way I've set it up? Is it the protected keyword?
There were a few reasons I was getting an error. Firstly register_activation_hooks()
is a tricky issue I had. It was not registering using my class structure (not shown above).
To solve this I made a quick fix by registering the activation in my plugin's main file.
The second was there needs to be a space between UNIQUE
and bracket and 2 spaces required between PRIMARY KEY
and bracket.
I also found that $wpdb->get_charset_collate()
was causing an error with my table creation so simply removed it for now.