wp_die()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
wp_die( string|WP_Error $message = '', string|int $title = '', string|array|int $args = array() ): never|void
Описание
Эта функция дополняет PHP-функцию die(). Отличие в том, что пользователю выводится HTML. Использовать её рекомендуется только тогда, когда выполнение не должно продолжаться дальше. Не рекомендуется вызывать эту функцию слишком часто; старайтесь обрабатывать как можно больше ошибок незаметно или более аккуратно.
Для краткости нужный код HTTP-ответа можно передать целым числом в параметр $title (тогда применяется заголовок по умолчанию) или в параметр $args.
Оригинал (английский)
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.
Параметры
$message
string|WP_Error
необязательный
= ''
$title
string|int
необязательный
= ''
$args
string|array|int
необязательный
= array()
Возвращаемое значение
never|void
Исходный код
wp-includes/functions.php:3781
function wp_die( $message = '', $title = '', $args = array() ) {
global $wp_query;
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 $callback Callback function name.
*/
$callback = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
} elseif ( wp_is_json_request() ) {
/**
* Filters the callback for killing WordPress execution for JSON requests.
*
* @since 5.1.0
*
* @param callable $callback Callback function name.
*/
$callback = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' );
} elseif ( wp_is_serving_rest_request() && wp_is_jsonp_request() ) {
/**
* Filters the callback for killing WordPress execution for JSONP REST requests.
*
* @since 5.2.0
*
* @param callable $callback Callback function name.
*/
$callback = apply_filters( 'wp_die_jsonp_handler', '_jsonp_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 $callback Callback function name.
*/
$callback = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
} elseif ( wp_is_xml_request()
|| isset( $wp_query ) &&
( function_exists( 'is_feed' ) && is_feed()
|| function_exists( 'is_comment_feed' ) && is_comment_feed()
|| function_exists( 'is_trackback' ) && is_trackback() ) ) {
/**
* Filters the callback for killing WordPress execution for XML requests.
*
* @since 5.2.0
*
* @param callable $callback Callback function name.
*/
$callback = apply_filters( 'wp_die_xml_handler', '_xml_wp_die_handler' );
} else {
/**
* Filters the callback for killing WordPress execution for all non-Ajax, non-JSON, non-XML requests.
*
* @since 3.0.0
*
* @param callable $callback Callback function name.
*/
$callback = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
}
call_user_func( $callback, $message, $title, $args );
}
История изменений
| Версия | Описание |
|---|---|
| 5.5.0 | The $text_direction argument has a priority over get_language_attributes() in the default handler. |
| 5.3.0 | The $charset argument was added. |
| 5.1.0 | The $link_url, $link_text, and $exit arguments were added. |
| 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. |

