返回介绍

wpmu_create_blog()

发布于 2017-09-11 11:18:07 字数 5468 浏览 1242 评论 0 收藏 0

wpmu_create_blog( string $domain,  string $path,  string $title,  int $user_id,  array $meta = array(),  int $site_id = 1 )

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


参数

$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.

$meta

(array) (Optional) Used to set initial site options.

Default value: array()

$site_id

(int) (Optional) Only relevant on multi-network installs.

Default value: 1


返回值

(int|WP_Error) Returns WP_Error object on failure, int $blog_id on success


源代码

File: wp-includes/ms-functions.php

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

更新日志

Versiondescription
MUIntroduced.

相关函数

Uses

  • wp-includes/load.php: wp_installing()
  • wp-admin/includes/upgrade.php: wp_install_defaults()
  • wp-includes/capabilities.php: is_super_admin()
  • wp-includes/cache.php: wp_cache_set()
  • wp-includes/l10n.php: __()
  • wp-includes/formatting.php: sanitize_user()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/plugin.php: do_action()
  • wp-includes/option.php: get_site_option()
  • wp-includes/option.php: update_option()
  • wp-includes/user.php: get_user_meta()
  • wp-includes/user.php: update_user_meta()
  • wp-includes/ms-functions.php: domain_exists()
  • wp-includes/ms-functions.php: insert_blog()
  • wp-includes/ms-functions.php: install_blog()
  • wp-includes/ms-functions.php: wpmu_new_blog
  • wp-includes/ms-functions.php: add_user_to_blog()
  • wp-includes/ms-load.php: is_subdomain_install()
  • wp-includes/ms-blogs.php: switch_to_blog()
  • wp-includes/ms-blogs.php: update_blog_status()
  • wp-includes/ms-blogs.php: restore_current_blog()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 17 more uses Hide more uses

Used By

  • wp-signup.php: validate_another_blog_signup()
  • wp-includes/ms-functions.php: wpmu_activate_signup()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文