Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is the code I am using to create a custom table named "prefix" + zillow--> where prefix is the wp table prefix for the site.

register_activation_hook(__FILE__,'CreateTable');

The actual function is given below-- it checks if such a table already exists along with assigned version number for that table(so that I can update /alter that table in a later version of this theme)--

function CreateTable() {
  //Database table versions
  $zillow_db_table_version= "1.0";
  $table_name = $wpdb->prefix . "zillow";

  $installed_ver = get_option( "zillow_db_table_version" );
  //Check if the table already exists and if the table is up to date, if not create it
  if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name || $installed_ver != $zillow_db_table_version ) {
    $sql = "CREATE TABLE " . $table_name .
    " (propertyid varchar(20) NOT NULL ,
    inserted_on date,
    UNIQUE KEY id (id) );";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
    update_option( "zillow_db_table_version", $zillow_db_table_version );
  }
  //Add database table versions to options
  add_option("zillow_db_table_version", $zillow_db_table_version);

}

But the table is not getting created at all. What is wrong with the above code? I tried activating the theme using above code multiple times(after first activating a different theme and then activating this one) but no table is getting created (I check directly in the WP database list of tables ).

share|improve this question
    
Have you tried adding global $wpdb; and turn on debugging. –  Wyck Jun 7 '13 at 17:28

1 Answer 1

dbDelta is extremely picky. Looking at your code I'd say you've violated this rule:

You must put each field on its own line in your SQL statement

Two of your fields are sharing lines with parenthesis. I haven't tested that particular pattern but dbDelta is extremely picky.

I don't know if that space after NOT NULL will matter.

I doubt that checking for the existence of the table is necessary. Checking your option should be sufficient.

And finally, register_activation_hook is for plugins, not themes. There is no similar specialized theme activation hook that I am aware of. There are a number of work-arounds that have been suggested if you search this site for them.

share|improve this answer
    
what do you suggest i use in place of dbDelta? Also the code sample in my question bunched up the code together-- actually in the php code the fields are all on different rows (including the key at the end of sql for table definition). –  Arvind Jun 7 '13 at 17:45
    
I don't suggest you use anything instead of dbDelta but you have to very, very careful with syntax. I think the primary answer is at the bottom though. You say in the title that you want to run this on theme activation but you are using a hook that runs on plugin activation. –  s_ha_dum Jun 7 '13 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.