函数文档

rest_handle_doing_it_wrong()

💡 云策文档标注

概述

rest_handle_doing_it_wrong() 是 WordPress REST API 中用于处理 _doing_it_wrong 错误的函数,主要面向开发者调试,通过设置 HTTP 头输出错误信息。

关键要点

  • 函数用于处理 _doing_it_wrong 错误,仅在 WP_DEBUG 启用且 HTTP 头未发送时执行。
  • 接受三个参数:$function_name(函数名)、$message(错误消息)、$version(WordPress 版本号,可为 null)。
  • 根据 $version 参数是否存在,格式化输出不同的翻译字符串,并设置 X-WP-DoingItWrong HTTP 头。

代码示例

rest_handle_doing_it_wrong( $function_name, $message, $version ) {
    if ( ! WP_DEBUG || headers_sent() ) {
        return;
    }

    if ( $version ) {
        /* translators: Developer debugging message. 1: PHP function name, 2: WordPress version number, 3: Explanatory message. */
        $string = __( '%1$s (since %2$s; %3$s)' );
        $string = sprintf( $string, $function_name, $version, $message );
    } else {
        /* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message. */
        $string = __( '%1$s (%2$s)' );
        $string = sprintf( $string, $function_name, $message );
    }

    header( sprintf( 'X-WP-DoingItWrong: %s', $string ) );
}

注意事项

  • 函数仅在调试模式下(WP_DEBUG 为 true)且 HTTP 头未发送时生效,否则直接返回。
  • 输出信息通过 X-WP-DoingItWrong HTTP 头传递,便于开发者监控和调试。
  • 自 WordPress 5.5.0 版本引入,相关功能如 __() 用于翻译字符串。

📄 原文内容

Handles _doing_it_wrong errors.

Parameters

$function_namestringrequired
The function that was called.
$messagestringrequired
A message explaining what has been done incorrectly.
$versionstring|nullrequired
The version of WordPress where the message was added.

Source

function rest_handle_doing_it_wrong( $function_name, $message, $version ) {
	if ( ! WP_DEBUG || headers_sent() ) {
		return;
	}

	if ( $version ) {
		/* translators: Developer debugging message. 1: PHP function name, 2: WordPress version number, 3: Explanatory message. */
		$string = __( '%1$s (since %2$s; %3$s)' );
		$string = sprintf( $string, $function_name, $version, $message );
	} else {
		/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message. */
		$string = __( '%1$s (%2$s)' );
		$string = sprintf( $string, $function_name, $message );
	}

	header( sprintf( 'X-WP-DoingItWrong: %s', $string ) );
}

Changelog

Version Description
5.5.0 Introduced.