wpmu_create_blog

The timeline below displays how wordpress function wpmu_create_blog has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Creates a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installations, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installations, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU (3.0.0)
 *
 * @param string $domain     The new site's domain.
 * @param string $path       The new site's path.
 * @param string $title      The new site's title.
 * @param int    $user_id    The user ID of the new site's admin.
 * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
 *                           If valid status keys are included ('public', 'archived', 'mature',
 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
 *                           updated. Otherwise, keys and values will be used to set options for
 *                           the new site. Default empty array.
 * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
 *                           Default 1.
 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $options = array(), $network_id = 1)
{
    $defaults = array('public' => 0);
    $options = wp_parse_args($options, $defaults);
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $network_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    $allowed_data_fields = array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    $site_data = array_merge(array('domain' => $domain, 'path' => $path, 'network_id' => $network_id), array_intersect_key($options, array_flip($allowed_data_fields)));
    // Data to pass to wp_initialize_site().
    $site_initialization_data = array('title' => $title, 'user_id' => $user_id, 'options' => array_diff_key($options, array_flip($allowed_data_fields)));
    $blog_id = wp_insert_site(array_merge($site_data, $site_initialization_data));
    if (is_wp_error($blog_id)) {
        return $blog_id;
    }
    wp_cache_set_sites_last_changed();
    return $blog_id;
}

WordPress Version: 6.2

/**
 * Creates a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installations, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installations, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU (3.0.0)
 *
 * @param string $domain     The new site's domain.
 * @param string $path       The new site's path.
 * @param string $title      The new site's title.
 * @param int    $user_id    The user ID of the new site's admin.
 * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
 *                           If valid status keys are included ('public', 'archived', 'mature',
 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
 *                           updated. Otherwise, keys and values will be used to set options for
 *                           the new site. Default empty array.
 * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
 *                           Default 1.
 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $options = array(), $network_id = 1)
{
    $defaults = array('public' => 0);
    $options = wp_parse_args($options, $defaults);
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $network_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    $allowed_data_fields = array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    $site_data = array_merge(array('domain' => $domain, 'path' => $path, 'network_id' => $network_id), array_intersect_key($options, array_flip($allowed_data_fields)));
    // Data to pass to wp_initialize_site().
    $site_initialization_data = array('title' => $title, 'user_id' => $user_id, 'options' => array_diff_key($options, array_flip($allowed_data_fields)));
    $blog_id = wp_insert_site(array_merge($site_data, $site_initialization_data));
    if (is_wp_error($blog_id)) {
        return $blog_id;
    }
    wp_cache_set('last_changed', microtime(), 'sites');
    return $blog_id;
}

WordPress Version: 6.1

/**
 * Creates a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installations, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installations, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU (3.0.0)
 *
 * @param string $domain     The new site's domain.
 * @param string $path       The new site's path.
 * @param string $title      The new site's title.
 * @param int    $user_id    The user ID of the new site's admin.
 * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
 *                           If valid status keys are included ('public', 'archived', 'mature',
 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
 *                           updated. Otherwise, keys and values will be used to set options for
 *                           the new site. Default empty array.
 * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $options = array(), $network_id = 1)
{
    $defaults = array('public' => 0);
    $options = wp_parse_args($options, $defaults);
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $network_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    $allowed_data_fields = array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    $site_data = array_merge(array('domain' => $domain, 'path' => $path, 'network_id' => $network_id), array_intersect_key($options, array_flip($allowed_data_fields)));
    // Data to pass to wp_initialize_site().
    $site_initialization_data = array('title' => $title, 'user_id' => $user_id, 'options' => array_diff_key($options, array_flip($allowed_data_fields)));
    $blog_id = wp_insert_site(array_merge($site_data, $site_initialization_data));
    if (is_wp_error($blog_id)) {
        return $blog_id;
    }
    wp_cache_set('last_changed', microtime(), 'sites');
    return $blog_id;
}

WordPress Version: 5.5

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installations, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installations, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU (3.0.0)
 *
 * @param string $domain     The new site's domain.
 * @param string $path       The new site's path.
 * @param string $title      The new site's title.
 * @param int    $user_id    The user ID of the new site's admin.
 * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
 *                           If valid status keys are included ('public', 'archived', 'mature',
 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
 *                           updated. Otherwise, keys and values will be used to set options for
 *                           the new site. Default empty array.
 * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $options = array(), $network_id = 1)
{
    $defaults = array('public' => 0);
    $options = wp_parse_args($options, $defaults);
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $network_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    $allowed_data_fields = array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    $site_data = array_merge(array('domain' => $domain, 'path' => $path, 'network_id' => $network_id), array_intersect_key($options, array_flip($allowed_data_fields)));
    // Data to pass to wp_initialize_site().
    $site_initialization_data = array('title' => $title, 'user_id' => $user_id, 'options' => array_diff_key($options, array_flip($allowed_data_fields)));
    $blog_id = wp_insert_site(array_merge($site_data, $site_initialization_data));
    if (is_wp_error($blog_id)) {
        return $blog_id;
    }
    wp_cache_set('last_changed', microtime(), 'sites');
    return $blog_id;
}

WordPress Version: 5.1

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installations, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installations, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU (3.0.0)
 *
 * @param string $domain     The new site's domain.
 * @param string $path       The new site's path.
 * @param string $title      The new site's title.
 * @param int    $user_id    The user ID of the new site's admin.
 * @param array  $options    Optional. Array of key=>value pairs used to set initial site options.
 *                           If valid status keys are included ('public', 'archived', 'mature',
 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
 *                           updated. Otherwise, keys and values will be used to set options for
 *                           the new site. Default empty array.
 * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $options = array(), $network_id = 1)
{
    $defaults = array('public' => 0);
    $options = wp_parse_args($options, $defaults);
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $network_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    $site_data_whitelist = array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    $site_data = array_merge(array('domain' => $domain, 'path' => $path, 'network_id' => $network_id), array_intersect_key($options, array_flip($site_data_whitelist)));
    // Data to pass to wp_initialize_site().
    $site_initialization_data = array('title' => $title, 'user_id' => $user_id, 'options' => array_diff_key($options, array_flip($site_data_whitelist)));
    $blog_id = wp_insert_site(array_merge($site_data, $site_initialization_data));
    if (is_wp_error($blog_id)) {
        return $blog_id;
    }
    wp_cache_set('last_changed', microtime(), 'sites');
    return $blog_id;
}

WordPress Version: 4.9

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installations, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installations, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU (3.0.0)
 *
 * @param string $domain     The new site's domain.
 * @param string $path       The new site's path.
 * @param string $title      The new site's title.
 * @param int    $user_id    The user ID of the new site's admin.
 * @param array  $meta       Optional. Array of key=>value pairs used to set initial site options.
 *                           If valid status keys are included ('public', 'archived', 'mature',
 *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
 *                           updated. Otherwise, keys and values will be used to set options for
 *                           the new site. Default empty array.
 * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
 * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $network_id = 1)
{
    $defaults = array('public' => 0, 'WPLANG' => get_network_option($network_id, 'WPLANG'));
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $network_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    if (!$blog_id = insert_blog($domain, $path, $network_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    /**
     * Fires immediately after a new site is created.
     *
     * @since MU (3.0.0)
     *
     * @param int    $blog_id    Site ID.
     * @param int    $user_id    User ID.
     * @param string $domain     Site domain.
     * @param string $path       Site path.
     * @param int    $network_id Network ID. Only relevant on multi-network installations.
     * @param array  $meta       Meta data. Used to set initial site options.
     */
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $network_id, $meta);
    wp_cache_set('last_changed', microtime(), 'sites');
    return $blog_id;
}

WordPress Version: 4.7

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installs, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU
 *
 * @param string $domain  The new site's domain.
 * @param string $path    The new site's path.
 * @param string $title   The new site's title.
 * @param int    $user_id The user ID of the new site's admin.
 * @param array  $meta    Optional. Used to set initial site options.
 * @param int    $site_id Optional. Only relevant on multi-network installs.
 * @return int|WP_Error Returns WP_Error object on failure, int $blog_id on success
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $site_id = 1)
{
    $defaults = array('public' => 0, 'WPLANG' => get_site_option('WPLANG'));
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $site_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    if (!$blog_id = insert_blog($domain, $path, $site_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    /**
     * Fires immediately after a new site is created.
     *
     * @since MU
     *
     * @param int    $blog_id Blog ID.
     * @param int    $user_id User ID.
     * @param string $domain  Site domain.
     * @param string $path    Site path.
     * @param int    $site_id Site ID. Only relevant on multi-network installs.
     * @param array  $meta    Meta data. Used to set initial site options.
     */
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
    wp_cache_set('last_changed', microtime(), 'sites');
    return $blog_id;
}

WordPress Version: 4.6

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to {@see 'wpmu_new_blog'}
 * for events that should affect all new sites.
 *
 * On subdirectory installs, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU
 *
 * @param string $domain  The new site's domain.
 * @param string $path    The new site's path.
 * @param string $title   The new site's title.
 * @param int    $user_id The user ID of the new site's admin.
 * @param array  $meta    Optional. Used to set initial site options.
 * @param int    $site_id Optional. Only relevant on multi-network installs.
 * @return int|WP_Error Returns WP_Error object on failure, int $blog_id on success
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $site_id = 1)
{
    $defaults = array('public' => 0);
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $site_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    if (!$blog_id = insert_blog($domain, $path, $site_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    add_option('WPLANG', get_site_option('WPLANG'));
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    /**
     * Fires immediately after a new site is created.
     *
     * @since MU
     *
     * @param int    $blog_id Blog ID.
     * @param int    $user_id User ID.
     * @param string $domain  Site domain.
     * @param string $path    Site path.
     * @param int    $site_id Site ID. Only relevant on multi-network installs.
     * @param array  $meta    Meta data. Used to set initial site options.
     */
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
    wp_cache_set('last_changed', microtime(), 'sites');
    return $blog_id;
}

WordPress Version: 4.4

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
 * for events that should affect all new sites.
 *
 * On subdirectory installs, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU
 *
 * @param string $domain  The new site's domain.
 * @param string $path    The new site's path.
 * @param string $title   The new site's title.
 * @param int    $user_id The user ID of the new site's admin.
 * @param array  $meta    Optional. Used to set initial site options.
 * @param int    $site_id Optional. Only relevant on multi-network installs.
 * @return int|WP_Error Returns WP_Error object on failure, int $blog_id on success
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $site_id = 1)
{
    $defaults = array('public' => 0);
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $site_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!wp_installing()) {
        wp_installing(true);
    }
    if (!$blog_id = insert_blog($domain, $path, $site_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    add_option('WPLANG', get_site_option('WPLANG'));
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    /**
     * Fires immediately after a new site is created.
     *
     * @since MU
     *
     * @param int    $blog_id Blog ID.
     * @param int    $user_id User ID.
     * @param string $domain  Site domain.
     * @param string $path    Site path.
     * @param int    $site_id Site ID. Only relevant on multi-network installs.
     * @param array  $meta    Meta data. Used to set initial site options.
     */
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
    return $blog_id;
}

WordPress Version: 4.3

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
 * for events that should affect all new sites.
 *
 * On subdirectory installs, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU
 *
 * @param string $domain  The new site's domain.
 * @param string $path    The new site's path.
 * @param string $title   The new site's title.
 * @param int    $user_id The user ID of the new site's admin.
 * @param array  $meta    Optional. Used to set initial site options.
 * @param int    $site_id Optional. Only relevant on multi-network installs.
 * @return int|WP_Error Returns WP_Error object on failure, int $blog_id on success
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $site_id = 1)
{
    $defaults = array('public' => 0);
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $site_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!defined('WP_INSTALLING')) {
        define('WP_INSTALLING', true);
    }
    if (!$blog_id = insert_blog($domain, $path, $site_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    add_option('WPLANG', get_site_option('WPLANG'));
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    /**
     * Fires immediately after a new site is created.
     *
     * @since MU
     *
     * @param int    $blog_id Blog ID.
     * @param int    $user_id User ID.
     * @param string $domain  Site domain.
     * @param string $path    Site path.
     * @param int    $site_id Site ID. Only relevant on multi-network installs.
     * @param array  $meta    Meta data. Used to set initial site options.
     */
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
    return $blog_id;
}

WordPress Version: 4.1

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
 * for events that should affect all new sites.
 *
 * On subdirectory installs, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU
 *
 * @param string $domain The new site's domain.
 * @param string $path The new site's path.
 * @param string $title The new site's title.
 * @param int $user_id The user ID of the new site's admin.
 * @param array $meta Optional. Used to set initial site options.
 * @param int $site_id Optional. Only relevant on multi-network installs.
 * @return mixed Returns WP_Error object on failure, int $blog_id on success
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $site_id = 1)
{
    $defaults = array('public' => 0);
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $site_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!defined('WP_INSTALLING')) {
        define('WP_INSTALLING', true);
    }
    if (!$blog_id = insert_blog($domain, $path, $site_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    add_option('WPLANG', get_site_option('WPLANG'));
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    /**
     * Fires immediately after a new site is created.
     *
     * @since MU
     *
     * @param int    $blog_id Blog ID.
     * @param int    $user_id User ID.
     * @param string $domain  Site domain.
     * @param string $path    Site path.
     * @param int    $site_id Site ID. Only relevant on multi-network installs.
     * @param array  $meta    Meta data. Used to set initial site options.
     */
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
    return $blog_id;
}

WordPress Version: 3.8

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
 * for events that should affect all new sites.
 *
 * On subdirectory installs, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU
 * @uses domain_exists()
 * @uses insert_blog()
 * @uses wp_install_defaults()
 * @uses add_user_to_blog()
 *
 * @param string $domain The new site's domain.
 * @param string $path The new site's path.
 * @param string $title The new site's title.
 * @param int $user_id The user ID of the new site's admin.
 * @param array $meta Optional. Used to set initial site options.
 * @param int $site_id Optional. Only relevant on multi-network installs.
 * @return mixed Returns WP_Error object on failure, int $blog_id on success
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $site_id = 1)
{
    $defaults = array('public' => 0);
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $site_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!defined('WP_INSTALLING')) {
        define('WP_INSTALLING', true);
    }
    if (!$blog_id = insert_blog($domain, $path, $site_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    add_option('WPLANG', get_site_option('WPLANG'));
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    /**
     * Fires immediately after a new site is created.
     *
     * @since MU
     *
     * @param int    $blog_id Blog ID.
     * @param int    $user_id User ID.
     * @param string $domain  Site domain.
     * @param string $path    Site path.
     * @param int    $site_id Site ID. Only relevant on multi-network installs.
     * @param array  $meta    Meta data. Used to set initial site options.
     */
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
    return $blog_id;
}

WordPress Version: 3.7

/**
 * Create a site.
 *
 * This function runs when a user self-registers a new site as well
 * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
 * for events that should affect all new sites.
 *
 * On subdirectory installs, $domain is the same as the main site's
 * domain, and the path is the subdirectory name (eg 'example.com'
 * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
 * root domain (eg 'blog1.example.com'), and $path is '/'.
 *
 * @since MU
 * @uses domain_exists()
 * @uses insert_blog()
 * @uses wp_install_defaults()
 * @uses add_user_to_blog()
 *
 * @param string $domain The new site's domain.
 * @param string $path The new site's path.
 * @param string $title The new site's title.
 * @param int $user_id The user ID of the new site's admin.
 * @param array $meta Optional. Used to set initial site options.
 * @param int $site_id Optional. Only relevant on multi-network installs.
 * @return mixed Returns WP_Error object on failure, int $blog_id on success
 */
function wpmu_create_blog($domain, $path, $title, $user_id, $meta = array(), $site_id = 1)
{
    $defaults = array('public' => 0);
    $meta = wp_parse_args($meta, $defaults);
    $domain = preg_replace('/\s+/', '', sanitize_user($domain, true));
    if (is_subdomain_install()) {
        $domain = str_replace('@', '', $domain);
    }
    $title = strip_tags($title);
    $user_id = (int) $user_id;
    if (empty($path)) {
        $path = '/';
    }
    // Check if the domain has been used already. We should return an error message.
    if (domain_exists($domain, $path, $site_id)) {
        return new WP_Error('blog_taken', __('Sorry, that site already exists!'));
    }
    if (!defined('WP_INSTALLING')) {
        define('WP_INSTALLING', true);
    }
    if (!$blog_id = insert_blog($domain, $path, $site_id)) {
        return new WP_Error('insert_blog', __('Could not create site.'));
    }
    switch_to_blog($blog_id);
    install_blog($blog_id, $title);
    wp_install_defaults($user_id);
    add_user_to_blog($blog_id, $user_id, 'administrator');
    foreach ($meta as $key => $value) {
        if (in_array($key, array('public', 'archived', 'mature', 'spam', 'deleted', 'lang_id'))) {
            update_blog_status($blog_id, $key, $value);
        } else {
            update_option($key, $value);
        }
    }
    add_option('WPLANG', get_site_option('WPLANG'));
    update_option('blog_public', (int) $meta['public']);
    if (!is_super_admin($user_id) && !get_user_meta($user_id, 'primary_blog', true)) {
        update_user_meta($user_id, 'primary_blog', $blog_id);
    }
    restore_current_blog();
    do_action('wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta);
    return $blog_id;
}