Using WordPress ‘install_blog()’ PHP function

The install_blog() WordPress PHP function installs an empty blog by creating new blog tables and options.

Usage

install_blog($blog_id, $blog_title = '');

Parameters

  • $blog_id (int) – Required. The value returned by wp_insert_site().
  • $blog_title (string) – Optional. The title of the new site. Default: ''.

More information

See WordPress Developer Resources: install_blog()

Examples

Installing a new blog with a given ID and title

// Use the 'wp_insert_site()' function to get the blog ID
$blog_id = wp_insert_site(array('domain' => 'example.com', 'path' => '/newblog/'));
$blog_title = 'My New Blog';

// Switch to the new blog before installing
switch_to_blog($blog_id);

// Install the new blog
install_blog($blog_id, $blog_title);

// Switch back to the original blog
restore_current_blog();

Installing a new blog without specifying a title

// Get the blog ID
$blog_id = wp_insert_site(array('domain' => 'example.com', 'path' => '/newblog/'));

// Switch to the new blog before installing
switch_to_blog($blog_id);

// Install the new blog without a title
install_blog($blog_id);

// Switch back to the original blog
restore_current_blog();

Installing multiple blogs with a loop

$blog_titles = array('Blog 1', 'Blog 2', 'Blog 3');

foreach ($blog_titles as $title) {
    // Get the blog ID
    $blog_id = wp_insert_site(array('domain' => 'example.com', 'path' => '/' . sanitize_title($title) . '/'));

    // Switch to the new blog before installing
    switch_to_blog($blog_id);

    // Install the new blog with the given title
    install_blog($blog_id, $title);

    // Switch back to the original blog
    restore_current_blog();
}

Installing a new blog and setting a theme

// Get the blog ID
$blog_id = wp_insert_site(array('domain' => 'example.com', 'path' => '/newblog/'));
$blog_title = 'My New Blog';

// Switch to the new blog before installing
switch_to_blog($blog_id);

// Install the new blog
install_blog($blog_id, $blog_title);

// Set a theme for the new blog
switch_theme('twentytwentyone');

// Switch back to the original blog
restore_current_blog();

Installing a new blog and adding a user

// Get the blog ID
$blog_id = wp_insert_site(array('domain' => 'example.com', 'path' => '/newblog/'));
$blog_title = 'My New Blog';

// Switch to the new blog before installing
switch_to_blog($blog_id);

// Install the new blog
install_blog($blog_id, $blog_title);

// Add a new user to the blog
$user_id = wp_create_user('johndoe', 'password123', '[email protected]');
add_user_to_blog($blog_id, $user_id, 'administrator');

// Switch back to the original blog
restore_current_blog();