get_status_header_desc()WP 2.3.0

Retrieve the description for the HTTP status.

Used By: status_header()
1 time — 0.00001 sec (speed of light) | 50000 times — 0.01 sec (speed of light) | PHP 7.0.8, WP 4.6.1

No Hooks.

Return

String. Status description if found, an empty string otherwise.

Usage

get_status_header_desc( $code );
$code(int) (required)
HTTP status code.

Examples

0

#1 Get a description of popular server response codes

echo get_status_header_desc( 200 ); //> OK

echo get_status_header_desc( 404 ); //> Not Found

echo get_status_header_desc( 301 ); //> Moved Permanently

echo get_status_header_desc( 500 ); //> Internal Server Error

All statuses described here or look in the function code.

0

#2 Example of dynamic wp_errors

function upload_handler( $update_media_item = false ) {

	if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
		return new WP_Error( 405, get_status_header_desc( 405 ), 405 );
	}

	if ( ! is_user_logged_in() ) {
		return new WP_Error( 403, get_status_header_desc( 403 ), 403 );
	}

	// some code
}
0

#3 Return WP_Error if code not 200

Snippet for your function, that do something with remote url and return WP_Error if response code not 200.

// ...

if ( $code !== 200 ) {

	unlink( $upload['file'] );

	return new WP_Error(
		'import_file_error',
		sprintf(
			__( 'Remote server returned %1$d %2$s for %3$s', 'wordpress-importer' ),
			$code,
			get_status_header_desc( $code ),
			$url
		)
	);
}

// ...

Notes

  • Global. Array. $wp_header_to_desc

Changelog

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

get_status_header_desc() code WP 6.7.1

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 '';
	}
}