函数文档

wp_check_php_mysql_versions()

💡 云策文档标注

概述

wp_check_php_mysql_versions() 函数用于检查服务器环境是否满足 WordPress 运行要求,包括 PHP 版本、PHP 扩展以及 MySQL/MariaDB 支持。如果条件不满足,函数会终止执行并显示错误信息。

关键要点

  • 检查 PHP 版本是否不低于 $required_php_version,否则返回 500 错误并退出。
  • 检查必需的 PHP 扩展(如 $required_php_extensions 数组定义)是否已加载,缺失时返回 500 错误并退出。
  • 检查 MySQL 扩展(mysqli_connect)或数据库 drop-in 文件(db.php)是否存在,缺失时调用 wp_die() 显示错误信息并退出。
  • 函数在默认常量定义前运行,因此需处理 WP_CONTENT_DIR 未定义的情况。

代码示例

function wp_check_php_mysql_versions() {
    global $required_php_version, $required_php_extensions, $wp_version;

    $php_version = PHP_VERSION;

    if ( version_compare( $required_php_version, $php_version, '>' ) ) {
        $protocol = wp_get_server_protocol();
        header( sprintf( '%s 500 Internal Server Error', $protocol ), true, 500 );
        header( 'Content-Type: text/html; charset=utf-8' );
        printf(
            'Your server is running PHP version %1$s but WordPress %2$s requires at least %3$s.',
            $php_version,
            $wp_version,
            $required_php_version
        );
        exit( 1 );
    }

    // 检查 PHP 扩展和 MySQL 支持的代码省略...
}

注意事项

  • 函数依赖全局变量 $required_php_version、$required_php_extensions 和 $wp_version,确保这些变量在调用前已正确定义。
  • 错误处理使用 HTTP 500 状态码和 wp_die(),适用于早期加载阶段,避免因环境问题导致 WordPress 无法启动。
  • 在检查 MySQL 支持时,优先验证 mysqli_connect 函数或 db.php 文件,以兼容不同数据库配置。

📄 原文内容

Checks the server requirements.

Description

  • PHP version
    • PHP extensions
    • MySQL or MariaDB version (unless a database drop-in is present)

Dies if requirements are not met.

Source

function wp_check_php_mysql_versions() {
	global $required_php_version, $required_php_extensions, $wp_version;

	$php_version = PHP_VERSION;

	if ( version_compare( $required_php_version, $php_version, '>' ) ) {
		$protocol = wp_get_server_protocol();
		header( sprintf( '%s 500 Internal Server Error', $protocol ), true, 500 );
		header( 'Content-Type: text/html; charset=utf-8' );
		printf(
			'Your server is running PHP version %1$s but WordPress %2$s requires at least %3$s.',
			$php_version,
			$wp_version,
			$required_php_version
		);
		exit( 1 );
	}

	$missing_extensions = array();

	if ( isset( $required_php_extensions ) && is_array( $required_php_extensions ) ) {
		foreach ( $required_php_extensions as $extension ) {
			if ( extension_loaded( $extension ) ) {
				continue;
			}

			$missing_extensions[] = sprintf(
				'WordPress %1$s requires the <code>%2$s</code> PHP extension.',
				$wp_version,
				$extension
			);
		}
	}

	if ( count( $missing_extensions ) > 0 ) {
		$protocol = wp_get_server_protocol();
		header( sprintf( '%s 500 Internal Server Error', $protocol ), true, 500 );
		header( 'Content-Type: text/html; charset=utf-8' );
		echo implode( '<br>', $missing_extensions );
		exit( 1 );
	}

	// This runs before default constants are defined, so we can't assume WP_CONTENT_DIR is set yet.
	$wp_content_dir = defined( 'WP_CONTENT_DIR' ) ? WP_CONTENT_DIR : ABSPATH . 'wp-content';

	if ( ! function_exists( 'mysqli_connect' )
		&& ! file_exists( $wp_content_dir . '/db.php' )
	) {
		require_once ABSPATH . WPINC . '/functions.php';
		wp_load_translations_early();

		$message = '<p>' . __( 'Your PHP installation appears to be missing the MySQL extension which is required by WordPress.' ) . "</p>n";

		$message .= '<p>' . sprintf(
			/* translators: %s: mysqli. */
			__( 'Please check that the %s PHP extension is installed and enabled.' ),
			'<code>mysqli</code>'
		) . "</p>n";

		$message .= '<p>' . sprintf(
			/* translators: %s: Support forums URL. */
			__( 'If you are unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href="%s">WordPress support forums</a>.' ),
			__( 'https://wordpress.org/support/forums/' )
		) . "</p>n";

		$args = array(
			'exit' => false,
			'code' => 'mysql_not_found',
		);
		wp_die(
			$message,
			__( 'Requirements Not Met' ),
			$args
		);
		exit( 1 );
	}
}

Changelog

Version Description
3.0.0 Introduced.