I am currently working on a personal project using the multisite capabilities of the alpha version of WordPress 3.0. When a new site is added to the WordPress network, a slew of new tables are added to the database, prefixed with the defined table prefix and the blog ID (eg: wp_2_).
Note: This article is about a development version of WordPress (3.0 alpha). Although the techniques presented here are likely to be valid in the publicly released version, please test this on a development site first.
For this project, I needed each new site to automatically have an additional table created that would hold information for the plugin I’m working on. The key is to tap into the wpmu_new_blog action, which fires directly after a new site is created.
The wpmu_new_blog action runs at the end of wpmu_create_blog(), a function located in /wp-includes/ms-functions.php which creates the new site. The action is passed two variables: $blog_id, which is the ID of the newly created site, and $user_id, which is the ID of the administrator of the site.
Here’s my finished code:
function nt_installTables($blog_id){
global $wpdb;
$table_name = $wpdb->get_blog_prefix($blog_id) . 'mytable';
$sql = "CREATE TABLE ". $table_name ." (
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
table_id varchar(20) NOT NULL,
page_id text NOT NULL,
html text NOT NULL,
snippet bigint(20) NOT NULL,
UNIQUE KEY ID (ID)) CHARSET=utf8;
";
require_once ABSPATH . '/wp-admin/includes/upgrade.php';
dbDelta($sql); //do it
}
add_action('wpmu_new_blog', 'nt_installTables');
Don’t forget you can also pass $user_id to your function.


Ron
March 19, 20103:33 pmYou should use <code>$wpdb->get_blog_prefix($blog_id) . 'test' instead</code> of <code>$wpdb->prefix . $blog_id . '_test'</code>.
johnkolbert
March 19, 20107:56 pmAh, thanks for pointing that out! I've updated the post to include your suggestion.
James
June 29, 20101:03 amThis is a great tutorial John. The latest version of wordpress is superb. Ron add a good note in the code. I will subscribe to your blog RSS.
Randy
August 13, 20102:46 amThanks for the tip! I used your basic idea to change some of the default settings (description, time zone, comment permissions, etc.) when a new blog is created.
John Kolbert
August 26, 201010:35 pmGlad you found it useful Randy!