WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp die

Description

Kill WordPress execution and conditionally display HTML message with error message.

A call to this function complements the die() PHP function. The difference is that HTML will be displayed to the user in the case of a typical web request. It is recommended to use this function only when the execution should not continue any further. It is not recommended to call this function very often and try to handle as many errors as possible silently.

If you really want to stop execution and exit the PHP script without outputting anything (and let the response time out) use exit() or die(). The function wp_die() is designed to give output just before it dies to avoid empty or time-outing responses.

Usage

<?php wp_die$message$title$args ?>

Parameters

$message
(mixed) (optional) Error message or a complete WP_Error object.
Default: None
$title
(string|int) (optional) Error title, If you use a WP_Error object, the title will be by default the one you added in $data['title'] (ignored when DOING_AJAX is true). If you pass an int, it is used as the response code (since 4.1).
Default: ''
$args
(string|array|int) (optional) Optional arguments to control behavior (ignored when DOING_AJAX is true). If you pass an int, it is used as the response code (since 4.1)
Default: None

Arguments

response
(integer) (optional) HTML status code returned.
Default: 500
back_link
(boolean) (optional) Whether to display a back link in the returned page.
Default: false
text_direction
(string) (optional) Whether to set ltr or rtl as the text direction
Default: 'ltr'

Return Values

(void) 
This function does not return a value.

Examples

Test to see what is in the $post variable in a filter:

add_filter( 'body_class', 'add_body_class_cb' );
/**
 * Add new body class.
 *
 * Testing what is in the $post variable.
 */
function add_body_class_cb( $classes ) {
    global $post;

    wp_die( '<pre>' . var_export( $post, true ) . '</pre>' );
}

Notes

  • Uses: apply_filters() - To filter the name of the die handler function:
    • Calls 'wp_die_ajax_handler' with the default AJAX die handler function ('_ajax_wp_die_handler') if the DOING_AJAX constant is set to true.
    • Calls 'wp_die_xmlrpc_handler' with the default XML-RPC die handler function ('_xmlrpc_wp_die_handler') if the XMLRPC_REQUEST constant is set to true.
    • Calls 'wp_die_handler' in every other case, with the default die handler function ('_default_wp_die_handler').
  • You can add a WP_Error object. If you've done so, you can add $data['title'] to the error object and it will automatically be taken as (default/overwriteable) title for the die page.

    [example missing here]

Change Log

Source File

wp_die() is located in wp-includes/functions.php.

Related

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.