wp_register_fatal_error_handler()
云策文档标注
概述
wp_register_fatal_error_handler() 函数用于注册致命错误的关闭处理程序。该处理程序仅在 wp_is_fatal_error_handler_enabled() 返回 true 时注册。
关键要点
- 函数注册一个关闭处理程序来处理致命错误。
- 注册前会检查 wp_is_fatal_error_handler_enabled() 是否启用。
- 优先尝试从 WP_CONTENT_DIR 加载自定义处理程序,否则使用默认的 WP_Fatal_Error_Handler。
- 通过 register_shutdown_function 调用处理程序的 handle 方法。
代码示例
function wp_register_fatal_error_handler() {
if ( ! wp_is_fatal_error_handler_enabled() ) {
return;
}
$handler = null;
if ( defined( 'WP_CONTENT_DIR' ) && is_readable( WP_CONTENT_DIR . '/fatal-error-handler.php' ) ) {
$handler = include WP_CONTENT_DIR . '/fatal-error-handler.php';
}
if ( ! is_object( $handler ) || ! is_callable( array( $handler, 'handle' ) ) ) {
$handler = new WP_Fatal_Error_Handler();
}
register_shutdown_function( array( $handler, 'handle' ) );
}注意事项
- 该函数在 WordPress 5.2.0 版本中引入。
- 相关函数 wp_is_fatal_error_handler_enabled() 用于检查处理程序是否启用。
原文内容
Registers the shutdown handler for fatal errors.
Description
The handler will only be registered if wp_is_fatal_error_handler_enabled() returns true.
Source
function wp_register_fatal_error_handler() {
if ( ! wp_is_fatal_error_handler_enabled() ) {
return;
}
$handler = null;
if ( defined( 'WP_CONTENT_DIR' ) && is_readable( WP_CONTENT_DIR . '/fatal-error-handler.php' ) ) {
$handler = include WP_CONTENT_DIR . '/fatal-error-handler.php';
}
if ( ! is_object( $handler ) || ! is_callable( array( $handler, 'handle' ) ) ) {
$handler = new WP_Fatal_Error_Handler();
}
register_shutdown_function( array( $handler, 'handle' ) );
}
Changelog
| Version | Description |
|---|---|
| 5.2.0 | Introduced. |