status_header()WP 2.0.0

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 — Continue
  • 101 — Switching Protocols
  • 102 — Processing
  • 105 — Name Not Resolved

2xx: Success:

  • 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

3xx: Redirection:

  • 300 — Multiple Choices
  • 301 — Moved Permanently
  • 302 — Moved Temporarily
  • 302 — Found
  • 303 — See Other
  • 304 — Not Modified
  • 305 — Use Proxy
  • 306 — reserved (code was only used in early specifications)
  • 307 — Temporary Redirect

4xx: Client Error:

  • 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 Large
  • 415 — Unsupported Media Type
  • 416 — Requested Range Not Satisfiable
  • 417 — Expectation Failed
  • 418 — I'm a teapot
  • 422 — Unprocessable Entity
  • 423 — Locked
  • 424 — Failed Dependency
  • 425 — Unordered Collection
  • 426 — Upgrade Required
  • 428 — Precondition Required
  • 429 — Too Many Requests
  • 431 — Request Header Fields Too Large
  • 434 — Requested host unavailable.
  • 449 — Retry With
  • 451 — Unavailable For Legal Reasons
  • 456 — Unrecoverable Error
  • 499 — Used by Nginx when the client closes the connection before receiving a response.

5xx: Server Error:

  • 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
  • 508 — Loop Detected
  • 509 — Bandwidth Limit Exceeded
  • 510 — Not Extended
  • 511 — Network Authentication Required
$description(string)
Description for the header. Since version 4.4.
Default: ''

Examples

1

#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 ); 
} );
0

#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.

status_header() code WP 7.0

function status_header( $code, $description = '' ) {
	if ( ! $description ) {
		$description = get_status_header_desc( $code );
	}

	if ( empty( $description ) ) {
		return;
	}

	$protocol      = wp_get_server_protocol();
	$status_header = "$protocol $code $description";
	if ( function_exists( 'apply_filters' ) ) {

		/**
		 * Filters an HTTP status header.
		 *
		 * @since 2.2.0
		 *
		 * @param string $status_header HTTP status header.
		 * @param int    $code          HTTP status code.
		 * @param string $description   Description for the status code.
		 * @param string $protocol      Server protocol.
		 */
		$status_header = apply_filters( 'status_header', $status_header, $code, $description, $protocol );
	}

	if ( ! headers_sent() ) {
		header( $status_header, true, $code );
	}
}