函数文档

do_action_deprecated()

💡 云策文档标注

概述

do_action_deprecated() 用于触发已弃用的动作钩子,替代 do_action() 调用,在触发时生成弃用通知并执行原始钩子。

关键要点

  • 当动作钩子被弃用时,使用 do_action_deprecated() 替代 do_action(),以触发弃用通知并保持向后兼容性。
  • 参数包括钩子名称、参数数组、弃用版本、可选的替代钩子和消息,用于详细记录弃用信息。
  • 内部调用 _deprecated_hook() 生成弃用通知,然后通过 do_action_ref_array() 执行原始钩子。

代码示例

function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
	if ( ! has_action( $hook_name ) ) {
		return;
	}

	_deprecated_hook( $hook_name, $version, $replacement, $message );

	do_action_ref_array( $hook_name, $args );
}

注意事项

  • 使用前需检查钩子是否已注册动作,避免不必要的弃用通知。
  • 弃用信息应清晰,包括版本和替代钩子,以帮助开发者迁移代码。

📄 原文内容

Fires functions attached to a deprecated action hook.

Description

When an action hook is deprecated, the do_action() call is replaced with do_action_deprecated() , which triggers a deprecation notice and then fires the original hook.

See also

Parameters

$hook_namestringrequired
The name of the action hook.
$argsarrayrequired
Array of additional function arguments to be passed to do_action() .
$versionstringrequired
The version of WordPress that deprecated the hook.
$replacementstringoptional
The hook that should have been used. Default empty.
$messagestringoptional
A message regarding the change. Default empty.

Source

function do_action_deprecated( $hook_name, $args, $version, $replacement = '', $message = '' ) {
	if ( ! has_action( $hook_name ) ) {
		return;
	}

	_deprecated_hook( $hook_name, $version, $replacement, $message );

	do_action_ref_array( $hook_name, $args );
}

Changelog

Version Description
4.6.0 Introduced.