admin管理员组

文章数量:1325236

I'm trying to create a custom plugin where I want create a table when the plugin gets activated. I have tried the following code but it is not creating the table in the database

function create_plugin_database_table() {
 global $wpdb;
 $table_name = $wpdb->prefix . 'sandbox';
 $sql = "CREATE TABLE $table_name (
 id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
 title varchar(50) NOT NULL,
 structure longtext NOT NULL,
 author longtext NOT NULL,
 PRIMARY KEY  (id)
 );";

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

register_activation_hook( __FILE__, 'create_plugin_database_table' );

I'm trying to create a custom plugin where I want create a table when the plugin gets activated. I have tried the following code but it is not creating the table in the database

function create_plugin_database_table() {
 global $wpdb;
 $table_name = $wpdb->prefix . 'sandbox';
 $sql = "CREATE TABLE $table_name (
 id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
 title varchar(50) NOT NULL,
 structure longtext NOT NULL,
 author longtext NOT NULL,
 PRIMARY KEY  (id)
 );";

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

register_activation_hook( __FILE__, 'create_plugin_database_table' );
Share Improve this question edited Mar 17, 2016 at 12:20 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Mar 17, 2016 at 8:55 Arun KumareshArun Kumaresh 1931 gold badge1 silver badge6 bronze badges 3
  • You got a well detailed tutorial in the Codex: codex.wordpress/Creating_Tables_with_Plugins – RRikesh Commented Mar 17, 2016 at 9:13
  • Tried your code on a fresh install, it works. – RRikesh Commented Mar 17, 2016 at 9:19
  • i have already installed the plugin and added this code – Arun Kumaresh Commented Mar 17, 2016 at 9:23
Add a comment  | 

3 Answers 3

Reset to default 18

You have to include wpadmin/upgrade-functions.php file to create a table example

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

    $tblname = 'pin';
    $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' );

This code works for me :

function installer(){
    include('installer.php');
}
register_activation_hook(__file__, 'installer');

Then installer.php :

global $wpdb;
$table_name = $wpdb->prefix . "my_products";
$my_products_db_version = '1.0.0';
$charset_collate = $wpdb->get_charset_collate();

if ( $wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name ) {

    $sql = "CREATE TABLE $table_name (
            ID mediumint(9) NOT NULL AUTO_INCREMENT,
            `product-model` text NOT NULL,
            `product-name` text NOT NULL,
            `product-description` int(9) NOT NULL,
            PRIMARY KEY  (ID)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
    add_option('my_db_version', $my_products_db_version);
}
function astro_plugin_table_install() {
    global $wpdb;
    global $charset_collate;
    $table_name = $wpdb->prefix . 'pin';
     $sql = "CREATE TABLE IF NOT EXISTS $table_name (
      `id` bigint(20) NOT NULL AUTO_INCREMENT,
      `pincode` bignit(128) DEFAULT NOT NULL,
       PRIMARY KEY (`id`)
    )$charset_collate;";
     require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     dbDelta( $sql );
}
register_activation_hook(__FILE__,'astro_plugin_table_install');

本文标签: Create a table in custom plugin on the activating it