_scalar_wp_die_handler()
云策文档标注
概述
_scalar_wp_die_handler() 是处理 APP 请求时 wp_die() 的处理器,用于终止 WordPress 执行并显示错误消息。
关键要点
- 此函数是 wp_die() 的处理器,专门用于 APP 请求场景。
- 接受三个参数:$message(可选字符串,默认空字符串)、$title(可选字符串,默认空字符串,但未使用)、$args(可选字符串或数组,默认空数组)。
- 通过 _wp_die_process_input() 处理输入参数,并根据 $parsed_args['exit'] 决定是否终止执行。
- 如果 $message 是标量,则输出或终止时转换为字符串。
代码示例
function _scalar_wp_die_handler( $message = '', $title = '', $args = array() ) {
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
if ( $parsed_args['exit'] ) {
if ( is_scalar( $message ) ) {
die( (string) $message );
}
die();
}
if ( is_scalar( $message ) ) {
echo (string) $message;
}
}注意事项
- 此函数在 WordPress 5.1.0 版本中增加了 $title 和 $args 参数,自 3.4.0 版本引入。
- 相关函数 _wp_die_process_input() 用于统一处理 wp_die() 的参数。
原文内容
Kills WordPress execution and displays an error message.
Description
This is the handler for wp_die() when processing APP requests.
Parameters
$messagestringoptional-
Response to print. Default empty string.
$titlestringoptional-
Error title (unused). Default empty string.
$argsstring|arrayoptional-
Arguments to control behavior.
Default:
array()
Source
function _scalar_wp_die_handler( $message = '', $title = '', $args = array() ) {
list( $message, $title, $parsed_args ) = _wp_die_process_input( $message, $title, $args );
if ( $parsed_args['exit'] ) {
if ( is_scalar( $message ) ) {
die( (string) $message );
}
die();
}
if ( is_scalar( $message ) ) {
echo (string) $message;
}
}