函数文档

wp_is_site_initialized()

💡 云策文档标注

概述

wp_is_site_initialized() 函数用于检查指定站点是否已初始化,即其数据库表是否存在。该函数支持通过站点ID或WP_Site对象进行查询,并返回布尔值。

关键要点

  • 函数接受 $site_id 参数,可以是整数或 WP_Site 对象,用于指定要检查的站点。
  • 返回值为布尔类型:true 表示站点已初始化,false 表示未初始化。
  • 内部实现通过检查数据库中的 posts 表是否存在来判断初始化状态。
  • 支持 pre_wp_is_site_initialized 过滤器,允许在数据库访问前自定义检查逻辑。
  • 涉及多站点切换操作,使用 switch_to_blog() 和 restore_current_blog() 来确保正确上下文。
  • 首次引入于 WordPress 5.1.0 版本。

代码示例

function wp_is_site_initialized( $site_id ) {
    global $wpdb;

    if ( is_object( $site_id ) ) {
        $site_id = $site_id->blog_id;
    }
    $site_id = (int) $site_id;

    $pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
    if ( null !== $pre ) {
        return (bool) $pre;
    }

    $switch = false;
    if ( get_current_blog_id() !== $site_id ) {
        $switch = true;
        remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
        switch_to_blog( $site_id );
    }

    $suppress = $wpdb->suppress_errors();
    $result   = (bool) $wpdb->get_results( "DESCRIBE {$wpdb->posts}" );
    $wpdb->suppress_errors( $suppress );

    if ( $switch ) {
        restore_current_blog();
        add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
    }

    return $result;
}

注意事项

  • 该函数主要用于多站点环境,检查特定站点的数据库表是否已创建。
  • 使用 pre_wp_is_site_initialized 过滤器时,返回非 null 值将跳过默认数据库检查。
  • 在切换博客上下文时,会临时移除 wp_switch_roles_and_user 动作以确保正确执行。
  • 函数内部抑制数据库错误以避免在表不存在时抛出异常。

📄 原文内容

Checks whether a site is initialized.

Description

A site is considered initialized when its database tables are present.

Parameters

$site_idint|WP_Siterequired
Site ID or object.

Return

bool True if the site is initialized, false otherwise.

Source

function wp_is_site_initialized( $site_id ) {
	global $wpdb;

	if ( is_object( $site_id ) ) {
		$site_id = $site_id->blog_id;
	}
	$site_id = (int) $site_id;

	/**
	 * Filters the check for whether a site is initialized before the database is accessed.
	 *
	 * Returning a non-null value will effectively short-circuit the function, returning
	 * that value instead.
	 *
	 * @since 5.1.0
	 *
	 * @param bool|null $pre     The value to return instead. Default null
	 *                           to continue with the check.
	 * @param int       $site_id The site ID that is being checked.
	 */
	$pre = apply_filters( 'pre_wp_is_site_initialized', null, $site_id );
	if ( null !== $pre ) {
		return (bool) $pre;
	}

	$switch = false;
	if ( get_current_blog_id() !== $site_id ) {
		$switch = true;
		remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
		switch_to_blog( $site_id );
	}

	$suppress = $wpdb->suppress_errors();
	$result   = (bool) $wpdb->get_results( "DESCRIBE {$wpdb->posts}" );
	$wpdb->suppress_errors( $suppress );

	if ( $switch ) {
		restore_current_blog();
		add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
	}

	return $result;
}

Hooks

apply_filters( ‘pre_wp_is_site_initialized’, bool|null $pre, int $site_id )

Filters the check for whether a site is initialized before the database is accessed.

Changelog

Version Description
5.1.0 Introduced.