My custom table not being created on wordpress plugin activation

I am trying to create my own custom table in wordpress database on plugin activation ..This is my code for that ..

function __construct()
    {
register_activation_hook(__FILE__,array(&$this, 'activate'));


    function activate()
    {

    global $wpdb;
    echo "<div class='updated'>Test Plugin Notice</div>";

    $table_name = $wpdb->prefix . "dive";

    $installed_ver = get_option( "divebook_db_table_dive_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 != $divebook_db_table_dive_version ) {
        $sql = "CREATE TABLE " . $table_name . " (
              id mediumint(9) NOT NULL AUTO_INCREMENT,
              date bigint(11) DEFAULT '0' NOT NULL,
              site tinytext NOT NULL,
              description text NOT NULL,
              max_depth mediumint(9) NOT NULL,
              time mediumint(9) NOT NULL,
              UNIQUE KEY id (id)
            );";

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
        update_option( "divebook_db_table_dive_version", $divebook_db_table_dive_version );

}
    //Add database table versions to options
    add_option("divebook_db_table_dive_version", $divebook_db_table_dive_version);

   }
}

I m checking my database .No new table has been created ..Plz look into it ..

Related posts

Leave a Reply

2 comments

  1. That’s because any WP activation/deactivation hook needs to be run from inside the plugin main file, not from a file you include in the main file.
    So, try to run the activation hook from your plugin-name.php file and it will work.

    L.E:
    also, i don’t see $divebook_db_table_dive_version being defined. Another thing, dbDelta caused problems for me in the past, try with $wpdb->query() to run the create table query.