wp_initialize_site

函数
wp_initialize_site ( $site_id, $args = array() )
参数
  • (int|WP_Site) $site_id Site ID or object.
    Required:
  • (array) $args { Optional. Arguments to modify the initialization behavior. @type int $user_id Required. User ID for the site administrator. @type string $title Site title. Default is 'Site %d' where %d is the site ID. @type array $options Custom option $key => $value pairs to use. Default empty array. @type array $meta Custom site metadata $key => $value pairs to use. Default empty array. }
    Required:
    Default: array()
返回值
  • (true|WP_Error) True on success, or error object on failure.
定义位置
相关方法
wp_uninitialize_sitewp_normalize_site_datawp_insert_sitewp_initial_constantswp_localize_script
引入
5.1.0
弃用
-

wp_initialize_site: 这个函数用来初始化一个新的WordPress站点。它设置了默认选项,并创建了新网站所需的初始页面和文章。

为一个给定的站点运行初始化程序。

这个过程包括创建网站的数据库表和用默认值填充它们。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_initialize_site( $site_id, array $args = array() ) {
global $wpdb, $wp_roles;
if ( empty( $site_id ) ) {
return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
}
$site = get_site( $site_id );
if ( ! $site ) {
return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
}
if ( wp_is_site_initialized( $site ) ) {
return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) );
}
$network = get_network( $site->network_id );
if ( ! $network ) {
$network = get_network();
}
$args = wp_parse_args(
$args,
array(
'user_id' => 0,
/* translators: %d: Site ID. */
'title' => sprintf( __( 'Site %d' ), $site->id ),
'options' => array(),
'meta' => array(),
)
);
/**
* Filters the arguments for initializing a site.
*
* @since 5.1.0
*
* @param array $args Arguments to modify the initialization behavior.
* @param WP_Site $site Site that is being initialized.
* @param WP_Network $network Network that the site belongs to.
*/
$args = apply_filters( 'wp_initialize_site_args', $args, $site, $network );
$orig_installing = wp_installing();
if ( ! $orig_installing ) {
wp_installing( true );
}
$switch = false;
if ( get_current_blog_id() !== $site->id ) {
$switch = true;
switch_to_blog( $site->id );
}
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
// Set up the database tables.
make_db_current_silent( 'blog' );
$home_scheme = 'http';
$siteurl_scheme = 'http';
if ( ! is_subdomain_install() ) {
if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
$home_scheme = 'https';
}
if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
$siteurl_scheme = 'https';
}
}
// Populate the site's options.
populate_options(
array_merge(
array(
'home' => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
'siteurl' => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
'blogname' => wp_unslash( $args['title'] ),
'admin_email' => '',
'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ),
'blog_public' => (int) $site->public,
'WPLANG' => get_network_option( $network->id, 'WPLANG' ),
),
$args['options']
)
);
// Clean blog cache after populating options.
clean_blog_cache( $site );
// Populate the site's roles.
populate_roles();
$wp_roles = new WP_Roles();
// Populate metadata for the site.
populate_site_meta( $site->id, $args['meta'] );
// Remove all permissions that may exist for the site.
$table_prefix = $wpdb->get_blog_prefix();
delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // Delete all.
delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all.
// Install default site content.
wp_install_defaults( $args['user_id'] );
// Set the site administrator.
add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
update_user_meta( $args['user_id'], 'primary_blog', $site->id );
}
if ( $switch ) {
restore_current_blog();
}
wp_installing( $orig_installing );
return true;
}
function wp_initialize_site( $site_id, array $args = array() ) { global $wpdb, $wp_roles; if ( empty( $site_id ) ) { return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) ); } $site = get_site( $site_id ); if ( ! $site ) { return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) ); } if ( wp_is_site_initialized( $site ) ) { return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) ); } $network = get_network( $site->network_id ); if ( ! $network ) { $network = get_network(); } $args = wp_parse_args( $args, array( 'user_id' => 0, /* translators: %d: Site ID. */ 'title' => sprintf( __( 'Site %d' ), $site->id ), 'options' => array(), 'meta' => array(), ) ); /** * Filters the arguments for initializing a site. * * @since 5.1.0 * * @param array $args Arguments to modify the initialization behavior. * @param WP_Site $site Site that is being initialized. * @param WP_Network $network Network that the site belongs to. */ $args = apply_filters( 'wp_initialize_site_args', $args, $site, $network ); $orig_installing = wp_installing(); if ( ! $orig_installing ) { wp_installing( true ); } $switch = false; if ( get_current_blog_id() !== $site->id ) { $switch = true; switch_to_blog( $site->id ); } require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // Set up the database tables. make_db_current_silent( 'blog' ); $home_scheme = 'http'; $siteurl_scheme = 'http'; if ( ! is_subdomain_install() ) { if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) { $home_scheme = 'https'; } if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) { $siteurl_scheme = 'https'; } } // Populate the site's options. populate_options( array_merge( array( 'home' => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ), 'siteurl' => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ), 'blogname' => wp_unslash( $args['title'] ), 'admin_email' => '', 'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ), 'blog_public' => (int) $site->public, 'WPLANG' => get_network_option( $network->id, 'WPLANG' ), ), $args['options'] ) ); // Clean blog cache after populating options. clean_blog_cache( $site ); // Populate the site's roles. populate_roles(); $wp_roles = new WP_Roles(); // Populate metadata for the site. populate_site_meta( $site->id, $args['meta'] ); // Remove all permissions that may exist for the site. $table_prefix = $wpdb->get_blog_prefix(); delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true ); // Delete all. delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all. // Install default site content. wp_install_defaults( $args['user_id'] ); // Set the site administrator. add_user_to_blog( $site->id, $args['user_id'], 'administrator' ); if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) { update_user_meta( $args['user_id'], 'primary_blog', $site->id ); } if ( $switch ) { restore_current_blog(); } wp_installing( $orig_installing ); return true; }
function wp_initialize_site( $site_id, array $args = array() ) {
	global $wpdb, $wp_roles;

	if ( empty( $site_id ) ) {
		return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
	}

	$site = get_site( $site_id );
	if ( ! $site ) {
		return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
	}

	if ( wp_is_site_initialized( $site ) ) {
		return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) );
	}

	$network = get_network( $site->network_id );
	if ( ! $network ) {
		$network = get_network();
	}

	$args = wp_parse_args(
		$args,
		array(
			'user_id' => 0,
			/* translators: %d: Site ID. */
			'title'   => sprintf( __( 'Site %d' ), $site->id ),
			'options' => array(),
			'meta'    => array(),
		)
	);

	/**
	 * Filters the arguments for initializing a site.
	 *
	 * @since 5.1.0
	 *
	 * @param array      $args    Arguments to modify the initialization behavior.
	 * @param WP_Site    $site    Site that is being initialized.
	 * @param WP_Network $network Network that the site belongs to.
	 */
	$args = apply_filters( 'wp_initialize_site_args', $args, $site, $network );

	$orig_installing = wp_installing();
	if ( ! $orig_installing ) {
		wp_installing( true );
	}

	$switch = false;
	if ( get_current_blog_id() !== $site->id ) {
		$switch = true;
		switch_to_blog( $site->id );
	}

	require_once ABSPATH . 'wp-admin/includes/upgrade.php';

	// Set up the database tables.
	make_db_current_silent( 'blog' );

	$home_scheme    = 'http';
	$siteurl_scheme = 'http';
	if ( ! is_subdomain_install() ) {
		if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
			$home_scheme = 'https';
		}
		if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
			$siteurl_scheme = 'https';
		}
	}

	// Populate the site's options.
	populate_options(
		array_merge(
			array(
				'home'        => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
				'siteurl'     => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
				'blogname'    => wp_unslash( $args['title'] ),
				'admin_email' => '',
				'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ),
				'blog_public' => (int) $site->public,
				'WPLANG'      => get_network_option( $network->id, 'WPLANG' ),
			),
			$args['options']
		)
	);

	// Clean blog cache after populating options.
	clean_blog_cache( $site );

	// Populate the site's roles.
	populate_roles();
	$wp_roles = new WP_Roles();

	// Populate metadata for the site.
	populate_site_meta( $site->id, $args['meta'] );

	// Remove all permissions that may exist for the site.
	$table_prefix = $wpdb->get_blog_prefix();
	delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true );   // Delete all.
	delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all.

	// Install default site content.
	wp_install_defaults( $args['user_id'] );

	// Set the site administrator.
	add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
	if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
		update_user_meta( $args['user_id'], 'primary_blog', $site->id );
	}

	if ( $switch ) {
		restore_current_blog();
	}

	wp_installing( $orig_installing );

	return true;
}

常见问题

FAQs
查看更多 >