1. 首页
  2. WordPress 函数手册

wp_die()

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

终止WordPress的执行并显示带有错误消息的HTML页面。
Kills WordPress execution and displays HTML page with an error message.

目录锚点:#说明#源码#笔记


说明(Description)

此函数补充die()PHP函数。区别在于,HTML将显示给用户。建议仅在不再继续执行时使用此函数。不建议经常调用此函数,并尝试以静默或更优雅的方式处理尽可能多的错误。简而言之,所需的HTTP响应代码可以作为整数传递给$title参数(默认的title将应用)或$args参数。


源码(Source)

/**
 * Kill WordPress execution and display HTML message with error message.
 *
 * 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.
 *
 * @since 2.0.4
 * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
 *              an integer to be used as the response code.
 *
 * @param string|WP_Error  $message Optional. Error message. If this is a {@see WP_Error} object,
 *                                  the error's messages are used. Default empty.
 * @param string|int       $title   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 empty.
 * @param string|array|int $args {
 *     Optional. Arguments to control behavior. If `$args` is an integer, then it is treated
 *     as the response code. Default empty array.
 *
 *     @type int    $response       The HTTP response code. Default 500.
 *     @type bool   $back_link      Whether to include a link to go back. Default false.
 *     @type string $text_direction 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 {@see is_rtl()}.
 * }
 */
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 ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
		/**
		 * Filter callback for killing WordPress execution for AJAX requests.
		 *
		 * @since 3.4.0
		 *
		 * @param callback $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
	} elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
		/**
		 * Filter callback for killing WordPress execution for XML-RPC requests.
		 *
		 * @since 3.4.0
		 *
		 * @param callback $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
	} else {
		/**
		 * Filter callback for killing WordPress execution for all non-AJAX, non-XML-RPC requests.
		 *
		 * @since 3.0.0
		 *
		 * @param callback $function Callback function name.
		 */
		$function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
	}

	call_user_func( $function, $message, $title, $args );
}
更新版本 源码位置 使用 被使用
5.3.0 wp-includes/functions.php 18 18

笔记(Notes)

测试以查看筛选器中$post变量中的内容:

wp_die() 为WP2原创文章,链接:https://www.wp2.cn/functions/wp_die/