wpmu_create_blog() WordPress Function

The wpmu_create_blog function is used to create a new blog on a WordPress Multisite network. This function takes a few parameters, the most important of which are the domain and path of the new blog. This function is useful for creating new blogs on a Multisite network automatically, for example when a new user signs up.

wpmu_create_blog( string $domain, string $path, string $title, int $user_id, array $options = array(), int $network_id = 1 ) #

Creates a site.


Description

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 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 ‘/’.


Top ↑

Parameters

$domain

(string)(Required)The new site's domain.

$path

(string)(Required)The new site's path.

$title

(string)(Required)The new site's title.

$user_id

(int)(Required)The user ID of the new site's admin.

$options

(array)(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 value: array()

$network_id

(int)(Optional) Network ID. Only relevant on multi-network installations.

Default value: 1


Top ↑

Return

(int|WP_Error) Returns WP_Error object on failure, the new site ID on success.


Top ↑

Source

File: wp-includes/ms-functions.php

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;
}


Top ↑

Changelog

Changelog
VersionDescription
MU (3.0.0)Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More