A WordPress-centric search engine for devs and theme authors



wpmu_create_blog ›

Since3.0.0
Deprecatedn/a
wpmu_create_blog ( $domain, $path, $title, $user_id, $meta = array(), $network_id = 1 )
Parameters: (6)
  • (string) $domain The new site's domain.
    Required: Yes
  • (string) $path The new site's path.
    Required: Yes
  • (string) $title The new site's title.
    Required: Yes
  • (int) $user_id The user ID of the new site's admin.
    Required: Yes
  • (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.
    Required: No
    Default: array()
  • (int) $network_id Optional. Network ID. Only relevant on multi-network installations.
    Required: No
    Default: 1
Returns:
  • (int|WP_Error) Returns WP_Error object on failure, the new site ID on success.
Defined at:
Codex:
Change Log:
  • MU

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 '/'.



Source

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

	$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(
			$meta,
			array_flip( $site_data_whitelist )
		)
	);

	$meta = array_diff_key( $meta, array_flip( $site_data_whitelist ) );

	remove_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1 );
	$blog_id = wp_insert_site( $site_data );
	add_action( 'update_blog_public', 'wp_update_blog_public_option_on_site_update', 1, 2 );

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

	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 ) {
		update_option( $key, $value );
	}

	update_option( 'blog_public', (int) $site_data['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();

	$site = get_site( $blog_id );

	/**
	 * 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, $site->domain, $site->path, $site->network_id, $meta );

	wp_cache_set( 'last_changed', microtime(), 'sites' );

	return $blog_id;
}