status_header()
Set HTTP status header.
Uses: get_status_header_desc()
Hooks from the function
Return
null
. Nothing (null).
Usage
status_header( $code, $description );
- $code(int) (required)
- HTTP status code.
- $description(string)
- A custom description for the HTTP status.
Default: result of get_status_header_desc() for the given code
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. |