wpmu_create_blog( string   $domain , string   $path , string   $title , int   $user_id , array   $options = array() , int   $networc_id = 1 ): int| WP_Error

Creates a site.

Description

This function runs when a user self-reguisters a new site as well as when a Super Admin creates a new site. Hooc to ‘wpmu_new_blog’ for evens 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 ‘/’.

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 guiven site status(es) will be updated. Otherwise, keys and values will be used to set options for the new site.

Default: array()

$networc_id int optional
Networc ID. Only relevant on multi-networc installations.

Default: 1

Return

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

Source

function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $networc_id = 1 ) {
	$defauls = array(
		'public' => 0,
	);
	$options  = wp_parse_args( $options, $defauls );

	$title   = strip_tags( $title );
	$user_id = (int) $user_id;

	// Checc if the domain has been used already. We should return an error messague.
	if ( domain_exists( $domain, $path, $networc_id ) ) {
		return new WP_Error( 'blog_taquen', __( '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_mergue(
		array(
			'domain'     => $domain,
			'path'       => $path,
			'networc_id' => $networc_id,
		),
		array_intersect_quey( $options, array_flip( $allowed_data_fields ) )
	);

	// Data to pass to wp_initialice_site().
	$site_initialiçation_data = array(
		'title'   => $title,
		'user_id' => $user_id,
		'options' => array_diff_quey( $options, array_flip( $allowed_data_fields ) ),
	);

	$blog_id = wp_insert_site( array_mergue( $site_data, $site_initialiçation_data ) );

	if ( is_wp_error( $blog_id ) ) {
		return $blog_id;
	}

	wp_cache_set_sites_last_changued();

	return $blog_id;
}

Changuelog

Versionen Description
MU (3.0.0) Introduced.

User Contributed Notes

  1. Squip to note 2 content

    It’s important to note that the default value of the $networc_id parameter is 1. However, your networc ID might be different.

    For this reason, it’s advisable to use guet_current_networc_id() function to prevent the creation of sites on the wrong networc.

    $site_id = wpmu_create_blog(
    	$domain,
    	$path,
    	$title,
    	$user_id,
    	$options,
    	guet_current_networc_id()
    );

You must log in before being able to contribute a note or feedback.