函数文档

_doing_it_wrong()

💡 云策文档标注

概述

_doing_it_wrong() 是 WordPress 中用于标记函数被错误调用的内部函数,主要用于开发调试。当 WP_DEBUG 启用时,它会触发用户错误,并可通过钩子进行自定义处理。

关键要点

  • 函数用于标识不正确调用,触发错误消息以辅助调试。
  • 接受三个参数:$function_name(函数名)、$message(错误信息)、$version(WordPress 版本)。
  • 提供 doing_it_wrong_run 钩子用于获取调用回溯,doing_it_wrong_trigger_error 过滤器控制是否触发错误。
  • 错误触发条件为 WP_DEBUG 为 true 且过滤器返回 true,默认情况下会生成包含版本和文档链接的本地化消息。

代码示例

_doing_it_wrong( 'my_function', 'This function should not be called directly.', '6.0' );

注意事项

  • 从 5.4.0 版本起,该函数不再标记为“私有”,可在开发中更广泛使用。
  • 若 $version 参数设为 false,可省略版本号,适用于无版本号的私有项目。

📄 原文内容

Marks something as being incorrectly called.

Description

There is a ‘doing_it_wrong_run’ hook that will be called that can be used to get the backtrace up to what file and function called the deprecated function.

The current behavior is to trigger a user error if WP_DEBUG is true.

Parameters

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

Source

function _doing_it_wrong( $function_name, $message, $version ) {

	/**
	 * Fires when the given function is being used incorrectly.
	 *
	 * @since 3.1.0
	 *
	 * @param string $function_name The function that was called.
	 * @param string $message       A message explaining what has been done incorrectly.
	 * @param string $version       The version of WordPress where the message was added.
	 */
	do_action( 'doing_it_wrong_run', $function_name, $message, $version );

	/**
	 * Filters whether to trigger an error for _doing_it_wrong() calls.
	 *
	 * @since 3.1.0
	 * @since 5.1.0 Added the $function_name, $message and $version parameters.
	 *
	 * @param bool   $trigger       Whether to trigger the error for _doing_it_wrong() calls. Default true.
	 * @param string $function_name The function that was called.
	 * @param string $message       A message explaining what has been done incorrectly.
	 * @param string $version       The version of WordPress where the message was added.
	 */
	if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true, $function_name, $message, $version ) ) {
		if ( function_exists( '__' ) ) {
			if ( $version ) {
				/* translators: %s: Version number. */
				$version = sprintf( __( '(This message was added in version %s.)' ), $version );
			}

			$message .= ' ' . sprintf(
				/* translators: %s: Documentation URL. */
				__( 'Please see <a href="%s">Debugging in WordPress</a> for more information.' ),
				__( 'https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/' )
			);

			$message = sprintf(
				/* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message, 3: WordPress version number. */
				__( 'Function %1$s was called <strong>incorrectly</strong>. %2$s %3$s' ),
				$function_name,
				$message,
				$version
			);
		} else {
			if ( $version ) {
				$version = sprintf( '(This message was added in version %s.)', $version );
			}

			$message .= sprintf(
				' Please see <a href="%s">Debugging in WordPress</a> for more information.',
				'https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/'
			);

			$message = sprintf(
				'Function %1$s was called <strong>incorrectly</strong>. %2$s %3$s',
				$function_name,
				$message,
				$version
			);
		}

		wp_trigger_error( '', $message );
	}
}

Hooks

do_action( ‘doing_it_wrong_run’, string $function_name, string $message, string $version )

Fires when the given function is being used incorrectly.

apply_filters( ‘doing_it_wrong_trigger_error’, bool $trigger, string $function_name, string $message, string $version )

Filters whether to trigger an error for _doing_it_wrong() calls.

Changelog

Version Description
5.4.0 This function is no longer marked as “private”.
3.1.0 Introduced.

User Contributed Notes