函数文档

ms_not_installed()

💡 云策文档标注

概述

ms_not_installed() 函数用于在 WordPress 多站点网络(Multisite)中,当博客表不存在时显示失败消息。它检查缺失的 $wpdb->site 表,并生成详细的错误信息,包括数据库连接问题和表缺失情况。

关键要点

  • 函数用途:在博客表不存在时显示错误消息,适用于多站点网络环境。
  • 参数:$domain(请求的域名)和 $path(请求的路径),均为必需字符串参数。
  • 核心逻辑:检查 $wpdb->site 表是否存在,如果不存在则输出表缺失消息,否则输出站点查找失败消息。
  • 错误处理:使用 wp_die() 终止执行并显示 500 响应码的错误页面。
  • 相关函数:涉及 wpdb::esc_like()、wp_load_translations_early()、dead_db() 等,用于数据库操作和错误处理。

代码示例

function ms_not_installed( $domain, $path ) {
	global $wpdb;

	if ( ! is_admin() ) {
		dead_db();
	}

	wp_load_translations_early();

	$title = __( 'Error establishing a database connection' );

	$msg   = '' . $title . '';
	$msg  .= '' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
	$msg  .= ' ' . __( 'If you are the owner of this network please check that your host’s database server is running properly and all tables are error free.' ) . '';
	$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->site ) );
	if ( ! $wpdb->get_var( $query ) ) {
		$msg .= '' . sprintf(
			/* translators: %s: Table name. */
			__( 'Database tables are missing. This means that your host’s database server is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
			'' . $wpdb->site . ''
		) . '';
	} else {
		$msg .= '' . sprintf(
			/* translators: 1: Site URL, 2: Table name, 3: Database name. */
			__( 'Could not find site %1$s. Searched for table %2$s in database %3$s. Is that right?' ),
			'' . rtrim( $domain . $path, '/' ) . '',
			'' . $wpdb->blogs . '',
			'' . DB_NAME . ''
		) . '';
	}
	$msg .= '' . __( 'What do I do now?' ) . ' ';
	$msg .= sprintf(
		/* translators: %s: Documentation URL. */
		__( 'Read the Debugging a WordPress Network article. Some of the suggestions there may help you figure out what went wrong.' ),
		__( 'https://developer.wordpress.org/advanced-administration/debug/debug-network/' )
	);
	$msg .= ' ' . __( 'If you are still stuck with this message, then check that your database contains the following tables:' ) . '';
	foreach ( $wpdb->tables( 'global' ) as $t => $table ) {
		if ( 'sitecategories' === $t ) {
			continue;
		}
		$msg .= '' . $table . '';
	}
	$msg .= '';

	wp_die( $msg, $title, array( 'response' => 500 ) );
}

注意事项

  • 该函数主要用于多站点网络,在非管理界面(!is_admin())时调用 dead_db() 处理数据库错误。
  • 错误消息包含国际化支持,使用 __() 函数进行翻译。
  • 版本变更:从 WordPress 4.4.0 开始添加了 $domain 和 $path 参数,3.0.0 版本引入此函数。

📄 原文内容

Displays a failure message.

Description

Used when a blog’s tables do not exist. Checks for a missing $wpdb->site table as well.

Parameters

$domainstringrequired
The requested domain for the error to reference.
$pathstringrequired
The requested path for the error to reference.

Source

function ms_not_installed( $domain, $path ) {
	global $wpdb;

	if ( ! is_admin() ) {
		dead_db();
	}

	wp_load_translations_early();

	$title = __( 'Error establishing a database connection' );

	$msg   = '<h1>' . $title . '</h1>';
	$msg  .= '<p>' . __( 'If your site does not display, please contact the owner of this network.' ) . '';
	$msg  .= ' ' . __( 'If you are the owner of this network please check that your host’s database server is running properly and all tables are error free.' ) . '</p>';
	$query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $wpdb->site ) );
	if ( ! $wpdb->get_var( $query ) ) {
		$msg .= '<p>' . sprintf(
			/* translators: %s: Table name. */
			__( '<strong>Database tables are missing.</strong> This means that your host’s database server is not running, WordPress was not installed properly, or someone deleted %s. You really should look at your database now.' ),
			'<code>' . $wpdb->site . '</code>'
		) . '</p>';
	} else {
		$msg .= '<p>' . sprintf(
			/* translators: 1: Site URL, 2: Table name, 3: Database name. */
			__( '<strong>Could not find site %1$s.</strong> Searched for table %2$s in database %3$s. Is that right?' ),
			'<code>' . rtrim( $domain . $path, '/' ) . '</code>',
			'<code>' . $wpdb->blogs . '</code>',
			'<code>' . DB_NAME . '</code>'
		) . '</p>';
	}
	$msg .= '<p><strong>' . __( 'What do I do now?' ) . '</strong> ';
	$msg .= sprintf(
		/* translators: %s: Documentation URL. */
		__( 'Read the <a href="%s" target="_blank">Debugging a WordPress Network</a> article. Some of the suggestions there may help you figure out what went wrong.' ),
		__( 'https://developer.wordpress.org/advanced-administration/debug/debug-network/' )
	);
	$msg .= ' ' . __( 'If you are still stuck with this message, then check that your database contains the following tables:' ) . '</p><ul>';
	foreach ( $wpdb->tables( 'global' ) as $t => $table ) {
		if ( 'sitecategories' === $t ) {
			continue;
		}
		$msg .= '<li>' . $table . '</li>';
	}
	$msg .= '</ul>';

	wp_die( $msg, $title, array( 'response' => 500 ) );
}

Changelog

Version Description
4.4.0 The $domain and $path parameters were added.
3.0.0 Introduced.