Using WordPress ‘install_blog_defaults()’ PHP function

The install_blog_defaults() WordPress PHP function sets the default settings for a new blog.

Usage

install_blog_defaults($blog_id, $user_id);

Parameters

  • $blog_id (int) – The ID of the blog, ignored in this function.
  • $user_id (int) – The user ID associated with the blog.

More information

See WordPress Developer Resources: install_blog_defaults()

Examples

Set default settings for a new blog

// Set blog defaults for a new blog with blog_id = 1 and user_id = 1
install_blog_defaults(1, 1);

Set blog defaults for a new blog after creation

// Create a new blog
$blog_id = wp_create_blog('newblog.example.com', '/');

// Set default settings for the new blog
install_blog_defaults($blog_id, get_current_user_id());

Set blog defaults for multiple new blogs

// Array of blog_ids
$blog_ids = array(1, 2, 3, 4, 5);

// Set blog defaults for all blogs in the array
foreach ($blog_ids as $blog_id) {
    install_blog_defaults($blog_id, get_current_user_id());
}

Set blog defaults for new blogs created by a specific user

// Get the user ID of a specific user by their username
$user_id = get_user_by('login', 'john')->ID;

// Create a new blog
$blog_id = wp_create_blog('johnblog.example.com', '/');

// Set blog defaults for the new blog created by the specific user
install_blog_defaults($blog_id, $user_id);

Set blog defaults for new blogs created within a custom function

function create_new_blog_with_defaults($domain, $path, $user_id) {
    // Create a new blog
    $blog_id = wp_create_blog($domain, $path);

    // Set blog defaults for the new blog
    install_blog_defaults($blog_id, $user_id);

    return $blog_id;
}

// Use the custom function to create a new blog and set its defaults
$new_blog_id = create_new_blog_with_defaults('customblog.example.com', '/', get_current_user_id());