0

I am trying to create a WordPress customize plugin and I'm stuck in creating a table in my database on activation of my plugin. I tried this code:

function create_plugin_database_table()
{
    global $table_prefix, $wpdb;

    $tblname = 'customer';
    $wp_track_table = $table_prefix . "$tblname ";

    #Check to see if the table exists already, if not, then create it

    if($wpdb->get_var( "show tables like '$wp_track_table'" ) != 
    $wp_track_table) 
    {

    $sql = "CREATE TABLE `". $wp_track_table . "` ( ";
    $sql .= "  `id`  int(11)   NOT NULL auto_increment, ";
    $sql .= "  `pincode`  int(128)   NOT NULL, ";
    $sql .= "  PRIMARY KEY `order_id` (`id`) "; 
    $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; ";
    require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
    dbDelta($sql);
    }
}

register_activation_hook( __FILE__, 'create_plugin_database_table' );
3
  • Can you show some of your tries? Commented Jun 2, 2017 at 0:16
  • I already edited my question, please check. Commented Jun 2, 2017 at 0:17
  • When i tried to deactivate my plugin and activate again it won`t create a table in my database. Commented Jun 2, 2017 at 0:18

2 Answers 2

3

First, I've changed a bit your creation table script. Read prefix from $wpdb, and do not check table, use CREATE IF NOT EXIST.

function create_plugin_database_table() {
  global $wpdb;

  $tblname = 'customer';
  $wp_track_table = $wpdb->prefix . "$tblname";

  $sql = "CREATE TABLE IF NOT EXISTS $wp_track_table ( ";
  $sql .= "  `id`  int(11)   NOT NULL auto_increment, ";
  $sql .= "  `pincode`  int(128)   NOT NULL, ";
  $sql .= "  PRIMARY KEY `order_id` (`id`) "; 
  $sql .= ") ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; ";
  require_once( ABSPATH . '/wp-admin/includes/upgrade.php' );
  dbDelta($sql);

}
register_activation_hook( __FILE__, 'create_plugin_database_table' );

Also, I've put the require out. If you need to insert some default values, do it AFTER create, not in the same request. If didn't work, please enable debug and check logs.

Sign up to request clarification or add additional context in comments.

6 Comments

There is an error: Plugin could not be activated because it triggered a fatal error. Fatal error: Call to undefined function dbDelta()
when i tried to change the require_once( ABSPATH . '/wp-admin/includes/upgrade.php' ); up to the dbDelta(sql); Activation success
Sorry, I haven't readed correctly the require. It's correct to do the require before function dbDelta, because these function are defined on this file. Response updated.
HI Sakura will you assist me with my plugin?
If you have another questions about how to do things, and always you have try, I can give any answer if I know how to help you. It's OK if you do more questions on SO
|
0

There is the possibility of a conflict due to version compatibility. You can use the below snippet to check what version of the system has been used to avoid conflict.

function my_plugin_create_db() {

global $wpdb;
$version = get_option( 'my_plugin_version', '1.0' );
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'my_analysis';

$sql = "CREATE TABLE $table_name (
    id mediumint(9) NOT NULL AUTO_INCREMENT,
    time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
    views smallint(5) NOT NULL,
    clicks smallint(5) NOT NULL,
    UNIQUE KEY id (id)
) $charset_collate;";

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

if ( version_compare( $version, '2.0' ) < 0 ) {
    $sql = "CREATE TABLE $table_name (
      id mediumint(9) NOT NULL AUTO_INCREMENT,
      time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
      views smallint(5) NOT NULL,
      clicks smallint(5) NOT NULL,
      blog_id smallint(5) NOT NULL,
      UNIQUE KEY id (id)
    ) $charset_collate;";
    dbDelta( $sql );

    update_option( 'my_plugin_version', '2.0' );        
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.