php 什么是 add_action( 'init
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21201266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is add_action( 'init
提问by Nadeem Akram
Why do we use this type of thing in wordpress? Can anyone explain it to me please? Why do we use init in wordpress functions? Or, what is init?
为什么我们在wordpress中使用这种类型的东西?谁能给我解释一下?为什么我们在 wordpress 函数中使用 init ?或者,什么是init?
回答by Chizzle
Add action is used instead of hard-coding a function into WordPress. The benefit to using add_action is that you allow core wordpress functions to keep track of what has been added, and by doing so, can override previously added functions by de-registering them later on.
使用添加操作而不是将功能硬编码到 WordPress 中。使用 add_action 的好处是您允许核心 wordpress 函数跟踪已添加的内容,并且通过这样做,可以通过稍后取消注册来覆盖以前添加的函数。
For example:
例如:
You download a plugin with a defined action/method named
您下载一个插件,其定义的操作/方法名为
add_action( 'init', 'crappy_method' );
You need to override the crappy function with your own:
您需要用自己的功能覆盖蹩脚的功能:
remove_action('init', 'crappy_method' );
add_action( 'init', 'my_even_crappier_method' );
By doing this you can copy the original method and customize it without changing the original files. This is very useful with plugins so that you can update them later on without losing your changes.
通过这样做,您可以复制原始方法并对其进行自定义,而无需更改原始文件。这对于插件非常有用,以便您以后可以更新它们而不会丢失您的更改。
回答by DDphp
USAGE:add_action( $hook, $function_to_add, $priority, $accepted_args );
用法:add_action( $hook, $function_to_add, $priority, $accepted_args );
Parameter:$hook (string) (required) The name of the action to which $function_to_add is hooked. Can also be the name of an action inside a theme or plugin file, or the special tag "all", in which case the function will be called for all hooks) Default: None
参数:$hook (string) (required) $function_to_add 被钩住的动作的名称。也可以是主题或插件文件中的操作名称,或特殊标签“all”,在这种情况下,将为所有钩子调用该函数)默认值:无
INIT HOOK:Runs after WordPress has finished loading but before any headers are sent. Useful for intercepting $_GET or $_POST triggers.
INIT HOOK:在 WordPress 完成加载之后但在发送任何标头之前运行。用于拦截 $_GET 或 $_POST 触发器。
For example, to act on $_POST data:
例如,要对 $_POST 数据进行操作:
add_action('init', 'process_post');
function process_post(){
if(isset($_POST['unique_hidden_field'])) {
// process $_POST data here
}
}

