wp_is_site_initialized

函式
wp_is_site_initialized ( $site_id )
引數
  • (int|WP_Site) $site_id Site ID or object.
    Required:
返回值
  • (bool) True if the site is initialized, false otherwise.
定義位置
相關方法
is_serializedwp_initialize_sitewp_not_installedwp_uninitialize_sitewp_is_writable
引入
5.1.0
棄用
-

wp_is_site_initialized: 這個函式用來檢查WordPress站點是否已經被初始化。如果網站已經被初始化,則返回true,否則返回false。

檢查一個站點是否被初始化。

當一個網站的資料庫表存在時,它被認為是初始化了。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >