WordPress挂钩中的匿名功能 [英] Anonymous functions in WordPress hooks
问题描述
WordPress挂钩可以通过两种方式使用:
WordPress hooks can be used in two ways:
-
使用回调函数名称和适当的函数
using callback function name and appropriate function
add_action( 'action_name', 'callback_function_name' );
function callback_function_name() {
// do something
}
使用匿名功能(关闭)
using anonymous function (closure)
add_action( 'action_name', function() {
// do something
} );
WordPress的使用方式有何不同?什么是首选方式,为什么?
Is there any difference for WordPress what way to use? What is prefered way and why?
推荐答案
The disadvantage of the anonymous function is that you're not able to remove the action with remove_action.
重要提示:要删除一个钩子,添加钩子时,$function_to_remove
和$priority
参数必须匹配.这适用于过滤器和操作.删除失败不会发出警告.
Important: To remove a hook, the $function_to_remove
and $priority
arguments must match when the hook was added. This goes for both filters and actions. No warning will be given on removal failure.
由于未定义function_to_remove
,因此无法将其删除.
Because you didn't define function_to_remove
, you can't remove it.
因此,您永远不要在其他人可能想要覆盖的插件或主题中使用此功能.
So you should never use this inside plugins or themes that somebody else might want to overwrite.
这篇关于WordPress挂钩中的匿名功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!