get_status_header_desc()
Gets the text (message) of the HTTP response status code. For example, "Not Found".
The response code is always an int, sometimes you need to get the description of this code. For example, for 451 it will be the text "Unavailable For Legal Reasons".
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.
Returns
String.
String- Description (text) of the status code.Empty string- if the description of the specified status was not found.
Usage
get_status_header_desc( $code );
- $code(integer) (required)
- HTTP status code, the description of which needs to be obtained.
Examples
#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.
#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
} #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. |