Support » Plugins and Hacks » Cannot seem to create a table

  • Hi everyone.

    I’m trying to create (my first real) plugin, but seem to be stuck at the step where I create my tables. Could someone provide any insight to what I’m doing wrong?

    $st_db_version = "0.1";
    function st_install(){
       global $wpdb;
       global $st_db_version;
       $table_name = $wpdb->prefix . "spinthat";
       if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
          $sql = "CREATE TABLE " . $table_name . " (
    	  st_id mediumint(9) NOT NULL AUTO_INCREMENT,
    	  st_time bigint(11) DEFAULT '0' NOT NULL,
    	  st_artist tinytext NOT NULL,
    	  st_album tinytext NOT NULL,
    	  st_rating mediumint(6) DEFAULT '0' NOT NULL,
    	  st_blurb text NOT NULL,
    	  st_img VARCHAR(100) NOT NULL,
    	  st_url VARCHAR(100) NOT NULL,
    	  PRIMARY KEY  (st_id)
          require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
          add_option("st_db_version", $st_db_version);
    add_action('activate_plugindir/wp_spinthat.php', 'st_install');

    Thanks a lot,

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’m having the same issue. Have you found a solution by any chance? Thanks.

    Jackosh, you have an error in the way how you specified the function to call upon activation of the plugin. That isn’t really your fault as the whole functionality is poorly documented, at best. I made the same mistake and it took me almost two hours to figure this out. :-/


    1) The correct call would be:
    add_action('activate_[the name of your plugin folder]/wp_spinthat.php', 'st_install');

    If your plugin is not in a sub-folder, the first argument needs to be ‘activate_wp_spinthat.php’. This really should be mentioned in the codex (even if it might seem obvious to experienced users).

    2) The way I understand it, we are not supposed to call add_action directly to register a function for a plugin activation hook. That’s what the function register_activation_hook() is for. Important: This function takes the same arguments, but without the ‘activate_’ part on the first.

    3) If you want to avoid hard-coding the folder and file names of your plugin (might change in the future and you would need to remember changing them here then), then you can use this as the first argument: plugin_basename(__FILE__)
    This function will automatically generate the folder and file name of the current file.

    So, for your example that would be:
    register_activation_hook(plugin_basename(__FILE__), 'st_install');

    4) If you intend to use plugin_basename() on a Windows machine (e.g. for local testing), have a look at –> There’s a bug in WP. It has been fixed and will apparently be part of WP 2.3. If you want to use plugin_basename right away, just apply the patch that’s attached to the bug report. Worked for me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Cannot seem to create a table’ is closed to new replies.