wp_die( string|WP_Error $message = '', string|int $title = '', string|array|int $args = array() )

Kill WordPress execution and display HTML message with error message.


Description Description

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 Parameters

$message

(string|WP_Error) (Optional) Error message. If this is a WP_Error object, and not an Ajax or XML-RPC request, the error's messages are used.

Default value: ''

$title

(string|int) (Optional) 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 value: ''

$args

(string|array|int) (Optional) Arguments to control behavior. If $args is an integer, then it is treated as the response code.

  • 'response'
    (int) The HTTP response code. Default 200 for Ajax requests, 500 otherwise.
  • 'back_link'
    (bool) Whether to include a link to go back. Default false.
  • 'text_direction'
    (string) The text direction. This is only useful internally, when WordPress is still loading and the site's locale is not set up yet. Accepts 'rtl'. Default is the value of is_rtl().

Default value: array()


Top ↑

Source Source

File: wp-includes/functions.php

function wp_die( $message = '', $title = '', $args = array() ) {

	if ( is_int( $args ) ) {
		$args = array( 'response' => $args );
	} elseif ( is_int( $title ) ) {
		$args  = array( 'response' => $title );
		$title = '';
	}

	if ( wp_doing_ajax() ) {
		/**
		 * Filters the callback for killing WordPress execution for Ajax requests.
		 *
		 * @since 3.4.0
		 *
		 * @param callable $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
	} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
		/**
		 * Filters the callback for killing WordPress execution for XML-RPC requests.
		 *
		 * @since 3.4.0
		 *
		 * @param callable $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
	} else {
		/**
		 * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests.
		 *
		 * @since 3.0.0
		 *
		 * @param callable $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
	}

	call_user_func( $function, $message, $title, $args );
}

Top ↑

Changelog Changelog

Changelog
Version Description
4.1.0 The $title and $args parameters were changed to optionally accept an integer to be used as the response code.
2.0.4 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

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

    /**
     * Add new body class.
     *
     * Testing what is in the $post variable.
     *
     * @param array $classes Body classes.
     */
    function wpdocs_add_body_class( $classes ) {
    	/** @global WP_Post $post */
    	global $post;
    
    	wp_die( '<pre>' . var_export( $post, true ) . '</pre>' );
    }
    add_filter( 'body_class', 'wpdocs_add_body_class' );
    

You must log in before being able to contribute a note or feedback.