wpmu_create_blog

函数
wpmu_create_blog ( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 )
参数
  • (string) $domain The new site's domain.
    Required:
  • (string) $path The new site's path.
    Required:
  • (string) $title The new site's title.
    Required:
  • (int) $user_id The user ID of the new site's admin.
    Required:
  • (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.
    Required:
    Default: array()
  • (int) $network_id Optional. Network ID. Only relevant on multi-network installations.
    Required:
    Default: 1
返回值
  • (int|WP_Error) Returns WP_Error object on failure, the new site ID on success.
定义位置
相关方法
wpmu_delete_blogwp_create_tagwpmu_create_userwp_create_noncewp_create_thumbnail
引入
-
弃用
-

wpmu_create_blog – 这个函数在网络中创建一个新的站点,它接受的参数包括站点地址、站点标题、新站点管理员的用户ID等等。

创建一个站点。

当用户自行注册一个新站点以及超级管理员创建一个新站点时,该函数会运行。钩住{@see ‘wpmu_new_blog’}的事件,应该会影响到所有的新站点。

在子目录安装中,$domain与主站点的域名相同,而路径是子目录的名称(例如’example.com’和’/blog1/’)。在子域安装中,$domain是新的子域+根域(例如’blog1.example.com’),而$path是’/’。

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

常见问题

FAQs
查看更多 >