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.

Can someone tell me what's wrong with this function?

function xattachments_db_install() {
   global $wpdb;
   $xattachments_db_version = "1.0";
   $table_name = $wpdb->prefix ."xattachments";

   $sql = "CREATE TABLE IF NOT EXISTS ".$table_name. " (
        'xattachments_id' int(10) unsigned NOT NULL,
        'xattachments_name'  varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
        'xattachments_title' varchar(200) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
        'post_id' int(10) unsigned NOT NULL,
        'xattachments_data_id' int(11) NOT NULL,
        PRIMARY KEY ('xattachments_id') 
   ) ENGINE=MyISAM DEFAULT CHARSET=CHARSET=utf8 COLLATE utf8_bin ;";

   require_once(ABSPATH.'wp-admin/includes/upgrade.php');
   dbDelta($sql);
   add_option("xattachments_db_version", $xattachments_db_version);     
}
register_activation_hook(__FILE__,'xattachments_db_install');

It doesn't generate any error message when I activate the plugin but the table is not created in the database.

This function is located at the very beginning of the plugin file.

Thank you.

share|improve this question
1  
Code is OK as first look. Its should work. try to debug what sql running in dbDelta. –  Oleg Butuzov Nov 25 '12 at 9:29
    
thank you Code Monkey. How would I do that? –  Bachir Messaouri Nov 25 '12 at 9:30
    
Find location of dbDelta, check what arguments it gets, test that arguments (its sql which should run). –  Oleg Butuzov Nov 25 '12 at 9:38

1 Answer 1

up vote 3 down vote accepted

This part in your table creation SQL looks wrong: DEFAULT CHARSET=CHARSET=utf8. It should just be DEFAULT CHARSET=utf8.

Also, dbDelta() is quite a delicate function. Don't ask me why, but you need to put a double space after the PRIMARY KEY keyword. You also need to get rid of the quotes around the field names because dbDelta() chokes on those. Don't use backticks neither.

And, of course, set WP_DEBUG to TRUE while debugging.

share|improve this answer
    
Thank you. Did all your recommendations and it worked. –  Bachir Messaouri Nov 25 '12 at 16:33

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.