函数文档

wp_get_extension_error_description()

💡 云策文档标注

概述

wp_get_extension_error_description() 函数用于获取扩展错误的可读描述,基于 error_get_last() 返回的数组生成格式化字符串。

关键要点

  • 参数 $error 为必需,应来自 error_get_last() 的错误详情数组。
  • 返回字符串形式的格式化错误描述,包含错误类型、行号、文件名和消息。
  • 函数内部将错误类型代码映射为常量名(如 E_ERROR),并利用翻译函数 __() 支持本地化。

代码示例

function wp_get_extension_error_description( $error ) {
    $constants   = get_defined_constants( true );
    $constants   = isset( $constants['Core'] ) ? $constants['Core'] : $constants['internal'];
    $core_errors = array();

    foreach ( $constants as $constant => $value ) {
        if ( str_starts_with( $constant, 'E_' ) ) {
            $core_errors[ $value ] = $constant;
        }
    }

    if ( isset( $core_errors[ $error['type'] ] ) ) {
        $error['type'] = $core_errors[ $error['type'] ];
    }

    /* translators: 1: Error type, 2: Error line number, 3: Error file name, 4: Error message. */
    $error_message = __( 'An error of type %1$s was caused in line %2$s of the file %3$s. Error message: %4$s' );

    return sprintf(
        $error_message,
        "{$error['type']}",
        "{$error['line']}",
        "{$error['file']}",
        "{$error['message']}"
    );
}

📄 原文内容

Get a human readable description of an extension’s error.

Parameters

$errorarrayrequired
Error details from error_get_last().

Return

string Formatted error description.

Source

function wp_get_extension_error_description( $error ) {
	$constants   = get_defined_constants( true );
	$constants   = isset( $constants['Core'] ) ? $constants['Core'] : $constants['internal'];
	$core_errors = array();

	foreach ( $constants as $constant => $value ) {
		if ( str_starts_with( $constant, 'E_' ) ) {
			$core_errors[ $value ] = $constant;
		}
	}

	if ( isset( $core_errors[ $error['type'] ] ) ) {
		$error['type'] = $core_errors[ $error['type'] ];
	}

	/* translators: 1: Error type, 2: Error line number, 3: Error file name, 4: Error message. */
	$error_message = __( 'An error of type %1$s was caused in line %2$s of the file %3$s. Error message: %4$s' );

	return sprintf(
		$error_message,
		"<code>{$error['type']}</code>",
		"<code>{$error['line']}</code>",
		"<code>{$error['file']}</code>",
		"<code>{$error['message']}</code>"
	);
}

Changelog

Version Description
5.2.0 Introduced.