函数文档

get_status_header_desc()

💡 云策文档标注

概述

get_status_header_desc() 函数用于根据 HTTP 状态码获取对应的状态描述。它通过全局数组 $wp_header_to_desc 映射状态码与描述,若未找到则返回空字符串。

关键要点

  • 参数:$code(必需),整数类型,表示 HTTP 状态码。
  • 返回值:字符串,找到状态描述时返回描述,否则返回空字符串。
  • 内部使用全局数组 $wp_header_to_desc 存储状态码与描述的映射,包括从 100 到 511 的常见 HTTP 状态码。
  • 函数首先使用 absint() 确保参数为非负整数。

代码示例

function get_status_header_desc( $code ) {
    global $wp_header_to_desc;

    $code = absint( $code );

    if ( ! isset( $wp_header_to_desc ) ) {
        $wp_header_to_desc = array(
            100 => 'Continue',
            // ... 其他状态码映射
            511 => 'Network Authentication Required',
        );
    }

    if ( isset( $wp_header_to_desc[ $code ] ) ) {
        return $wp_header_to_desc[ $code ];
    } else {
        return '';
    }
}

注意事项

  • 函数在 WordPress 6.6.0 版本中添加了状态码 425,其他版本也有相应更新,如 5.1.0 添加 103,4.5.0 添加 308、421、451 等。
  • 相关函数包括 absint()、status_header() 等,用于整数转换和设置 HTTP 状态头。
  • 该函数被多个 WordPress 核心类和方法使用,如 WP_oEmbed_Controller 和 WP_HTTP_Requests_Response。

📄 原文内容

Retrieves the description for the HTTP status.

Parameters

$codeintrequired
HTTP status code.

Return

string Status description if found, an empty string otherwise.

Source

function get_status_header_desc( $code ) {
	global $wp_header_to_desc;

	$code = absint( $code );

	if ( ! isset( $wp_header_to_desc ) ) {
		$wp_header_to_desc = array(
			100 => 'Continue',
			101 => 'Switching Protocols',
			102 => 'Processing',
			103 => 'Early Hints',

			200 => 'OK',
			201 => 'Created',
			202 => 'Accepted',
			203 => 'Non-Authoritative Information',
			204 => 'No Content',
			205 => 'Reset Content',
			206 => 'Partial Content',
			207 => 'Multi-Status',
			226 => 'IM Used',

			300 => 'Multiple Choices',
			301 => 'Moved Permanently',
			302 => 'Found',
			303 => 'See Other',
			304 => 'Not Modified',
			305 => 'Use Proxy',
			306 => 'Reserved',
			307 => 'Temporary Redirect',
			308 => 'Permanent Redirect',

			400 => 'Bad Request',
			401 => 'Unauthorized',
			402 => 'Payment Required',
			403 => 'Forbidden',
			404 => 'Not Found',
			405 => 'Method Not Allowed',
			406 => 'Not Acceptable',
			407 => 'Proxy Authentication Required',
			408 => 'Request Timeout',
			409 => 'Conflict',
			410 => 'Gone',
			411 => 'Length Required',
			412 => 'Precondition Failed',
			413 => 'Request Entity Too Large',
			414 => 'Request-URI Too Long',
			415 => 'Unsupported Media Type',
			416 => 'Requested Range Not Satisfiable',
			417 => 'Expectation Failed',
			418 => 'I'm a teapot',
			421 => 'Misdirected Request',
			422 => 'Unprocessable Entity',
			423 => 'Locked',
			424 => 'Failed Dependency',
			425 => 'Too Early',
			426 => 'Upgrade Required',
			428 => 'Precondition Required',
			429 => 'Too Many Requests',
			431 => 'Request Header Fields Too Large',
			451 => 'Unavailable For Legal Reasons',

			500 => 'Internal Server Error',
			501 => 'Not Implemented',
			502 => 'Bad Gateway',
			503 => 'Service Unavailable',
			504 => 'Gateway Timeout',
			505 => 'HTTP Version Not Supported',
			506 => 'Variant Also Negotiates',
			507 => 'Insufficient Storage',
			510 => 'Not Extended',
			511 => 'Network Authentication Required',
		);
	}

	if ( isset( $wp_header_to_desc[ $code ] ) ) {
		return $wp_header_to_desc[ $code ];
	} else {
		return '';
	}
}

Changelog

Version Description
6.6.0 Added status code 425.
5.1.0 Added status code 103.
4.5.0 Added status codes 308, 421, and 451.
3.9.0 Added status codes 418, 428, 429, 431, and 511.
2.3.0 Introduced.