status_header()
Sets the specified status in the HTTP response header of the server. HTTP status code (200, 404, etc.).
For all pages, WordPress automatically sets HTTP status headers: 200 for existing pages, 404 for non-existing ones, etc.
To override the set header, the function must be called during the wp action or later, for example: template_redirect, template_include, etc.
The function is used when setting the WordPress request environment in the WP class before the wp action in the methods: WP::send_headers() and WP::handle_404()
The function should be used before any information is output to the screen. Otherwise, you will receive a PHP error. According to PHP rules, headers must be set before content output.
This function is a wrapper for the PHP header() function.
Hooks from the function
Returns
null. Does not return anything.
Usage
status_header( $header, $description );
- $header(integer) (required)
HTTP status code. See detailed descriptions of each status here.
Possible status codes:
1xx: Informational:
100— Continue101— Switching Protocols102— Processing105— Name Not Resolved
2xx: Success:
200— OK201— Created202— Accepted203— Non-Authoritative Information204— No Content205— Reset Content206— Partial Content207— Multi-Status226— IM Used
3xx: Redirection:
300— Multiple Choices301— Moved Permanently302— Moved Temporarily302— Found303— See Other304— Not Modified305— Use Proxy306— reserved (code was only used in early specifications)307— Temporary Redirect
4xx: Client Error:
400— Bad Request401— Unauthorized402— Payment Required403— Forbidden404— Not Found405— Method Not Allowed406— Not Acceptable407— Proxy Authentication Required408— Request Timeout409— Conflict410— Gone411— Length Required412— Precondition Failed413— Request Entity Too Large414— Request-URI Too Large415— Unsupported Media Type416— Requested Range Not Satisfiable417— Expectation Failed418— I'm a teapot422— Unprocessable Entity423— Locked424— Failed Dependency425— Unordered Collection426— Upgrade Required428— Precondition Required429— Too Many Requests431— Request Header Fields Too Large434— Requested host unavailable.449— Retry With451— Unavailable For Legal Reasons456— Unrecoverable Error499— Used by Nginx when the client closes the connection before receiving a response.
5xx: Server Error:
500— Internal Server Error501— Not Implemented502— Bad Gateway503— Service Unavailable504— Gateway Timeout505— HTTP Version Not Supported506— Variant Also Negotiates507— Insufficient Storage508— Loop Detected509— Bandwidth Limit Exceeded510— Not Extended511— Network Authentication Required
- $description(string)
- Description for the header. Since version 4.4.
Default: ''
Examples
#1 Сlose all site from indexing
Suppose you want to change the status code for all pages: you need to close all pages of the site from indexing.
To do this, add this code to functions.php:
add_action( 'wp', function(){
status_header( 503 );
} ); #2 Setting the 404 status to a specific post
Suppose we need to set 404 status for all post types event, depending on the status. I.e. we have an event, if it is not active, we need to give it a 404 status and display a 404 page code for it.
add_action( 'template_redirect', 'rr_404_my_event', 1 );
// this code prints a 404 page for an inactive event
// and sets the status to 404
function rr_404_my_event() {
global $post, $wp_query;
if ( is_singular( 'event' ) && ! rr_event_should_be_available( $post->ID ) ) {
// put a status
$wp_query->set_404();
status_header(404);
// output the 404.php file
include( get_query_template( '404' ) );
exit;
}
}
The rrr_event_should_be_available() function checks if the current post should be available.
Notes
Changelog
| Since 2.0.0 | Introduced. |
| Since 4.4.0 | Added the $description parameter. |