函数文档

is_blog_installed()

💡 云策文档标注

概述

is_blog_installed() 函数用于检测 WordPress 是否已安装,通过检查缓存和数据库选项(特别是 'siteurl')来实现。如果数据库表存在问题,函数会提示修复或返回 false。

关键要点

  • 函数首先检查 wp_cache_get('is_blog_installed') 缓存,若存在则直接返回 true。
  • 若未在安装模式(wp_installing() 返回 false),则加载所有自动加载选项(wp_load_alloptions()),检查 'siteurl' 选项是否存在。
  • 如果 'siteurl' 未自动加载,则通过 $wpdb->get_var() 查询数据库获取其值。
  • 若 'siteurl' 为空,函数会遍历 WordPress 表($wpdb->tables()),检查是否存在有效表,以判断是否允许全新安装或需要修复。
  • 在检测到数据库表不可用时,会调用 dead_db() 显示错误信息,提示修复数据库。
  • 函数最终返回布尔值,表示站点是否已安装,并更新缓存(wp_cache_set())。

代码示例

function is_blog_installed() {
	global $wpdb;

	if ( wp_cache_get( 'is_blog_installed' ) ) {
		return true;
	}

	$suppress = $wpdb->suppress_errors();

	if ( ! wp_installing() ) {
		$alloptions = wp_load_alloptions();
	}

	if ( ! isset( $alloptions['siteurl'] ) ) {
		$installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
	} else {
		$installed = $alloptions['siteurl'];
	}

	$wpdb->suppress_errors( $suppress );

	$installed = ! empty( $installed );
	wp_cache_set( 'is_blog_installed', $installed );

	if ( $installed ) {
		return true;
	}

	if ( defined( 'WP_REPAIRING' ) ) {
		return true;
	}

	$suppress = $wpdb->suppress_errors();

	$wp_tables = $wpdb->tables();
	foreach ( $wp_tables as $table ) {
		if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE === $table ) {
			continue;
		}

		if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE === $table ) {
			continue;
		}

		$described_table = $wpdb->get_results( "DESCRIBE $table;" );
		if (
			( ! $described_table && empty( $wpdb->last_error ) ) ||
			( is_array( $described_table ) && 0 === count( $described_table ) )
		) {
			continue;
		}

		wp_load_translations_early();

		$wpdb->error = sprintf(
			__( 'One or more database tables are unavailable. The database may need to be repaired.' ),
			'maint/repair.php?referrer=is_blog_installed'
		);

		dead_db();
	}

	$wpdb->suppress_errors( $suppress );

	wp_cache_set( 'is_blog_installed', false );

	return false;
}

注意事项

  • 函数依赖于缓存机制,如果使用默认 WordPress 缓存且数据库失效,可能导致误判。
  • 在修复模式(WP_REPAIRING 定义)下,函数会返回 true,允许修复流程接管。
  • 遍历表时,会跳过自定义用户表(CUSTOM_USER_TABLE 和 CUSTOM_USER_META_TABLE),以避免干扰安装状态判断。
  • 函数涉及多个 WordPress 核心函数和类方法,如 wpdb 相关操作,需确保环境正确配置。

📄 原文内容

Determines whether WordPress is already installed.

Description

The cache will be checked first. If you have a cache plugin, which saves the cache values, then this will work. If you use the default WordPress cache, and the database goes away, then you might have problems.

Checks for the ‘siteurl’ option for whether WordPress is installed.

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Return

bool Whether the site is already installed.

Source

function is_blog_installed() {
	global $wpdb;

	/*
	 * Check cache first. If options table goes away and we have true
	 * cached, oh well.
	 */
	if ( wp_cache_get( 'is_blog_installed' ) ) {
		return true;
	}

	$suppress = $wpdb->suppress_errors();

	if ( ! wp_installing() ) {
		$alloptions = wp_load_alloptions();
	}

	// If siteurl is not set to autoload, check it specifically.
	if ( ! isset( $alloptions['siteurl'] ) ) {
		$installed = $wpdb->get_var( "SELECT option_value FROM $wpdb->options WHERE option_name = 'siteurl'" );
	} else {
		$installed = $alloptions['siteurl'];
	}

	$wpdb->suppress_errors( $suppress );

	$installed = ! empty( $installed );
	wp_cache_set( 'is_blog_installed', $installed );

	if ( $installed ) {
		return true;
	}

	// If visiting repair.php, return true and let it take over.
	if ( defined( 'WP_REPAIRING' ) ) {
		return true;
	}

	$suppress = $wpdb->suppress_errors();

	/*
	 * Loop over the WP tables. If none exist, then scratch installation is allowed.
	 * If one or more exist, suggest table repair since we got here because the
	 * options table could not be accessed.
	 */
	$wp_tables = $wpdb->tables();
	foreach ( $wp_tables as $table ) {
		// The existence of custom user tables shouldn't suggest an unwise state or prevent a clean installation.
		if ( defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE === $table ) {
			continue;
		}

		if ( defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE === $table ) {
			continue;
		}

		$described_table = $wpdb->get_results( "DESCRIBE $table;" );
		if (
			( ! $described_table && empty( $wpdb->last_error ) ) ||
			( is_array( $described_table ) && 0 === count( $described_table ) )
		) {
			continue;
		}

		// One or more tables exist. This is not good.

		wp_load_translations_early();

		// Die with a DB error.
		$wpdb->error = sprintf(
			/* translators: %s: Database repair URL. */
			__( 'One or more database tables are unavailable. The database may need to be <a href="%s">repaired</a>.' ),
			'maint/repair.php?referrer=is_blog_installed'
		);

		dead_db();
	}

	$wpdb->suppress_errors( $suppress );

	wp_cache_set( 'is_blog_installed', false );

	return false;
}

Changelog

Version Description
2.1.0 Introduced.