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.

I am writing a plugin that on the activation must create a new table in database.

To do this i am using:

register_activation_hook( __FILE__, 'createTable' );

When i run the activation, my plugin creates the new table in the database but when the plugin-page is reloaded my plugin is listed as not active. So i have still the links:

activate|delete.

Why this happends? how to fix?

Here is my function:

    function createTable(){


    $link = mysql_connect('localhost', 'user', 'pass');
    if (!$link) {
        die( 'Could not connect: ' . mysql_error() );
    }
    mysql_select_db (db_name); 

    $sql = "CREATE TABLE IF NOT EXISTS UsersExtra (
      user_id INT NOT NULL,
      first_name VARCHAR(25) NOT NULL,
      last_name VARCHAR(25) NOT NULL,
      address VARCHAR(80) NOT NULL,
      city VARCHAR(30) NOT NULL,
      province CHAR(2) NOT NULL,
      postcode CHAR(7) NOT NULL,
      interests TEXT NULL,
      bio TEXT NULL,
      signature TEXT NULL,
      skills TEXT NULL,
      PRIMARY KEY (user_id),
      FULLTEXT KEY (interests, skills)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";

    mysql_query($sql) or die( mysql_error() );
    mysql_close($link);

}
share|improve this question
    
there must be some error in createTable function - turn on wp_debug in your wp-config and debug it on your own, or provide us with source code of that function –  david.binda Feb 21 '13 at 12:09
    
What does FILE means in register_activation_hook( FILE, 'createTable' );? Are code fragments you've pasted both resides in the main plugin file? –  Max Yudin Feb 21 '13 at 12:35
1  
@MaxYudin Problem was just misformating. Our dear OP still doesn't get how to use the editor (after asking 12 questions). –  kaiser Feb 21 '13 at 12:58
    
Btw: Use dbDelta() to create the table as shown here. –  kaiser Feb 21 '13 at 12:59
    
activating debug mode in wp-config, when i try to activate my plugin i get this message:The plugin generated 754 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin. –  Salvatore Dibenedetto Feb 21 '13 at 13:32

1 Answer 1

Works perfectly when this code is in main plugin file:

<?php
function createTable(){
    global $wpdb;
    $query = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix ."UsersExtra (
        user_id INT NOT NULL,
        first_name VARCHAR(25) NOT NULL,
        last_name VARCHAR(25) NOT NULL,
        address VARCHAR(80) NOT NULL,
        city VARCHAR(30) NOT NULL,
        province CHAR(2) NOT NULL,
        postcode CHAR(7) NOT NULL,
        interests TEXT NULL,
        bio TEXT NULL,
        signature TEXT NULL,
        skills TEXT NULL,
        PRIMARY KEY (user_id),
        FULLTEXT KEY (interests, skills)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8";
    $wpdb->query($query);
}
register_activation_hook(__FILE__, 'createTable');
share|improve this answer
    
Thank you! this is working fine! –  Salvatore Dibenedetto Feb 21 '13 at 13:45

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.