wp_debug_backtrace_summary()
云策文档标注
概述
wp_debug_backtrace_summary() 函数用于获取从代码起点到当前执行点的函数调用链,返回逗号分隔的字符串或数组。它支持忽略特定类、跳过堆栈帧和格式化输出,常用于调试和追踪调用来源。
关键要点
- 返回函数调用链,可选择字符串或数组格式
- 参数 $ignore_class 用于忽略指定类的所有函数调用
- 参数 $skip_frames 用于跳过指定数量的堆栈帧,便于回溯问题源头
- 参数 $pretty 控制输出格式,true 返回逗号分隔字符串,false 返回原始数组
- 内部使用 debug_backtrace() 并处理类方法、Hook 函数和文件包含调用
- 路径会被截断以简化输出,基于 WP_CONTENT_DIR 和 ABSPATH
代码示例
error_log( wp_debug_backtrace_summary() );
原文内容
Returns a comma-separated string or array of functions that have been called to get to the current point in code.
Description
See also
Parameters
$ignore_classstringoptional-
A class to ignore all function calls within – useful when you want to just give info about the callee.
Default:
null $skip_framesintoptional-
A number of stack frames to skip – useful for unwinding back to the source of the issue. Default 0.
$prettybooloptional-
Whether you want a comma separated string instead of the raw array returned.
Default:
true
Source
function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pretty = true ) {
static $truncate_paths;
$trace = debug_backtrace( false );
$caller = array();
$check_class = ! is_null( $ignore_class );
++$skip_frames; // Skip this function.
if ( ! isset( $truncate_paths ) ) {
$truncate_paths = array(
wp_normalize_path( WP_CONTENT_DIR ),
wp_normalize_path( ABSPATH ),
);
}
foreach ( $trace as $call ) {
if ( $skip_frames > 0 ) {
--$skip_frames;
} elseif ( isset( $call['class'] ) ) {
if ( $check_class && $ignore_class === $call['class'] ) {
continue; // Filter out calls.
}
$caller[] = "{$call['class']}{$call['type']}{$call['function']}";
} else {
if ( in_array( $call['function'], array( 'do_action', 'apply_filters', 'do_action_ref_array', 'apply_filters_ref_array' ), true ) ) {
$caller[] = "{$call['function']}('{$call['args'][0]}')";
} elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ), true ) ) {
$filename = isset( $call['args'][0] ) ? $call['args'][0] : '';
$caller[] = $call['function'] . "('" . str_replace( $truncate_paths, '', wp_normalize_path( $filename ) ) . "')";
} else {
$caller[] = $call['function'];
}
}
}
if ( $pretty ) {
return implode( ', ', array_reverse( $caller ) );
} else {
return $caller;
}
}
Changelog
| Version | Description |
|---|---|
| 3.4.0 | Introduced. |
Skip to note 2 content
Ulrich
Source