函数文档

install_blog()

💡 云策文档标注

概述

install_blog() 函数用于安装一个空博客,创建新博客的表和选项。该函数自 WordPress 5.1.0 起已被弃用,建议开发者使用替代方法。

关键要点

  • 函数功能:安装空博客,创建数据库表和设置选项。
  • 弃用状态:自 WordPress 5.1.0 起弃用,调用时需注意兼容性。
  • 使用前提:直接调用前应使用 switch_to_blog() 确保 $wpdb 指向新博客。
  • 参数说明:$blog_id(必需,来自 wp_insert_site() 的返回值)和 $blog_title(必需,新站点标题)。
  • 内部操作:包括检查安装状态、更新数据库、设置选项(如 siteurl、home、blogname)和清理用户权限。

注意事项

  • 该函数已弃用,新代码应避免使用,参考相关票务(如 Trac #27499)了解替代方案。
  • 调用时需确保数据库环境正确,否则可能导致安装失败或数据冲突。

📄 原文内容

Install an empty blog.

Description

Creates the new blog tables and options. If calling this function directly, be sure to use switch_to_blog() first, so that $wpdb points to the new blog.

Parameters

$blog_idintrequired
The value returned by wp_insert_site() .
$blog_titlestringrequired
The title of the new site.

Source

function install_blog( $blog_id, $blog_title = '' ) {
	global $wpdb, $wp_roles;

	_deprecated_function( __FUNCTION__, '5.1.0' );

	// Cast for security.
	$blog_id = (int) $blog_id;

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

	$suppress = $wpdb->suppress_errors();
	if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) ) {
		die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
	}
	$wpdb->suppress_errors( $suppress );

	$url = get_blogaddress_by_id( $blog_id );

	// Set everything up.
	make_db_current_silent( 'blog' );
	populate_options();
	populate_roles();

	// populate_roles() clears previous role definitions so we start over.
	$wp_roles = new WP_Roles();

	$siteurl = $home = untrailingslashit( $url );

	if ( ! is_subdomain_install() ) {

		if ( 'https' === parse_url( get_site_option( 'siteurl' ), PHP_URL_SCHEME ) ) {
			$siteurl = set_url_scheme( $siteurl, 'https' );
		}
		if ( 'https' === parse_url( get_home_url( get_network()->site_id ), PHP_URL_SCHEME ) ) {
			$home = set_url_scheme( $home, 'https' );
		}
	}

	update_option( 'siteurl', $siteurl );
	update_option( 'home', $home );

	if ( get_site_option( 'ms_files_rewriting' ) ) {
		update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
	} else {
		update_option( 'upload_path', get_blog_option( get_network()->site_id, 'upload_path' ) );
	}

	update_option( 'blogname', wp_unslash( $blog_title ) );
	update_option( 'admin_email', '' );

	// Remove all permissions.
	$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.
}

Changelog

Version Description
5.1.0 Deprecated.
MU (3.0.0) Introduced.

User Contributed Notes