• starapple

    (@starapple)


    I’ve read the codex on creating a table in the database when installing a plugin and seen the example there as well as on other sites with tutorials. My question is how to create multiple tables. The example function creates one table:

    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 need to create several tables. Do I create 10 functions for each CREATE TABLE statement or is there some way to loop through an SQL file and create the tables?

    Thanks.

Viewing 1 replies (of 1 total)
  • catacaustic

    (@catacaustic)

    It’s pretty easy. Just call dbDelta() for each new table that you want to create.

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    
    $sql = 'CREATE TABLE table1...';
    dbDelta ($sql);
    
    $sql = 'CREATE TABLE table2...';
    dbDelta ($sql);
    
    $sql = 'CREATE TABLE table3...';
    dbDelta ($sql);
Viewing 1 replies (of 1 total)
  • The topic ‘How to create multiple wpdb tables during plugin installation’ is closed to new replies.