status_header

Home » Functions » status_header

Function Name: status_header

The status_header function is a WordPress core function that allows developers to set the HTTP status header for a particular page or post. This function is used to notify clients about the current status of the page or post, such as whether it was found or not, whether it has been moved permanently or temporarily, or if there was an error in processing the request.

The status_header function is typically used in conjunction with other functions like header and wp_die to create custom error pages or to redirect users to a different page based on certain conditions.

Here’s an example usage code for the status_header function:

if ( ! is_user_logged_in() ) {
    status_header( 401 );
    nocache_headers();
    include( get_query_template( '401' ) );
    exit;
}

In this example, the status_header function is used to set the HTTP status code to 401 Unauthorized. The nocache_headers function is then used to prevent caching of the response, and the get_query_template function is used to load a custom error template for the 401 status code. Finally, the exit function is used to stop further processing of the script.

By using the status_header function, developers can provide a better user experience by communicating the status of the page or post to the client.

Learn More on WordPress.org

WordPress snippets using the status_header function

Register an account to save your snippets or go Pro to get more features.