WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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') 

   add_option("xattachments_db_version", $xattachments_db_version);     

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
Code is OK as first look. Its should work. try to debug what sql running in dbDelta. – qwerty qwerty 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). – qwerty qwerty Nov 25 '12 at 9:38
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


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.