wp_die

Kill WordPress execution and display HTML message with error message.

Description

wp_die( (string) $message = '', (string) $title = '', (array) $args = array() ); 

This function complements the die() PHP function. The difference is that HTML will be displayed to the user. 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 or more gracefully.

As a shorthand, the desired HTTP response code may be passed as an integer to the $title parameter (the default title would apply) or the $args parameter.

Parameters (3)

0. $message — Optional. (string) => ''
Error message. If this is a WP_Error object, and not an Ajax or XML-RPC request, the error's messages are used. Default empty.
1. $title — Optional. (string) => ''
Error title. If $message is a WP_Error object, error data with the key title may be used to specify the title. If $title is an integer, then it is treated as the response code. Default empty.
2. $args — Optional. (array) => array()
Arguments to control behavior. If $args is an integer, then it is treated as the response code. Default empty array.

Options

  • response (int) => 200 for Ajax requests, 500 otherwise

    The HTTP response code.

  • back_link (bool) => false

    Whether to include a link to go back.

array(

    /**
     * The HTTP response code.
     *
     * @type int
     * @default 200 for Ajax requests, 500 otherwise
     */
    'response' => 200 for Ajax requests, 500 otherwise,

    /**
     * Whether to include a link to go back.
     *
     * @type bool
     * @default false
     */
    'back_link' => false
);        


Usage

  1. if ( !function_exists( 'wp_die' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3. $message = ''; 
  4. $title = ''; 
  5.  
  6. // Optional. Arguments to control behavior. If `$args` is an integer, then it is treated as the response code. Default empty array. 
  7. $args = array( 
  8. 'response' => 200 for Ajax requests, 500 otherwise, 
  9. 'back_link' => false 
  10. ); 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = wp_die($message, $title, $args); 
  14.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function wp_die( $message = '', $title = '', $args = array() ) { 
  2.  
  3. if ( is_int( $args ) ) { 
  4. $args = array( 'response' => $args ); 
  5. } elseif ( is_int( $title ) ) { 
  6. $args = array( 'response' => $title ); 
  7. $title = ''; 
  8.  
  9. if ( wp_doing_ajax() ) { 
  10. /** 
  11. * Filters the callback for killing WordPress execution for Ajax requests. 
  12. * @since 3.4.0 
  13. * @param callable $function Callback function name. 
  14. */ 
  15. } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { 
  16. /** 
  17. * Filters the callback for killing WordPress execution for XML-RPC requests. 
  18. * @since 3.4.0 
  19. * @param callable $function Callback function name. 
  20. */ 
  21. } else { 
  22. /** 
  23. * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests. 
  24. * @since 3.0.0 
  25. * @param callable $function Callback function name. 
  26. */ 
  27.  
  28. call_user_func( $function, $message, $title, $args );