WordPress函数:add_action(添加动作)

说明

将函数连接到指定action(动作)。

Plugin API/Action Reference 上查看动作hook列表。wordpress核心调用do_action() 时触发动作。

用法

<?php
   <a href="https://www.wpdaxue.com/tag/add_action" title="查看与【add_action】相关的文章" target="_blank" rel="noopener">add_action</a>( $tag, $function_to_add, $priority,
         $accepted_args );
?&gt;

参数

$tag

(字符串)(必填)$function_to_add  所挂载的动作(action)的名称。(在Plugin API/Action Reference 上查看动作hook列表)。也可以是一个主题或插件文件内部的一个动作,或者特定的标签“all”,这个函数将被所有的钩子(hooks)调用。

默认值:None

$function_to_add

(回调)(必填)你希望挂载的函数的名称。注:在 PHP“回调”类型文档中 所罗列的字符串格式化的语法均可用。

默认值:None

$priority

(整数)(可选)用于指定与特定的动作相关联的函数的执行顺序。数字越小,执行越早,具有相同优先级的函数在它们被添加到动作的顺序执行。

默认值:10

$accepted_args

(整数)(可选)挂钩函数所接受的参数数量。在 WordPress1.5.1 及以后的版本中,挂钩函数可以是调用do_action() 或 apply_filters()时设置的参数。例如,comment_id_not_found动作将传递任何函数,若该函数将所请求的评论编号连接到该动作。

默认值:1

返回值

(布尔)总是True。

示例

简单的挂钩

博客发表新内容时用电子邮件通知朋友:

function email_friends( $post_ID )  
{
   $friends = 'bob@example.org, susie@example.org';
   wp_mail( $friends, &quot;sally's blog updated&quot;, 'I just put something on my blog: http://blog.example.com' );
 
   return $post_ID;
}
<a href="https://www.wpdaxue.com/tag/add_action" title="查看与【add_action】相关的文章" target="_blank" rel="noopener">add_action</a>( 'publish_post', 'email_friends' );

接受的参数

挂钩函数可以选择接受从动作调用的参数,如果有任何要传递的话。在这个简单的例子中,echo_comment_id  函数需要 $comment_id 参数,该参数将在 comment_id_not_found 过滤钩子运行时通过 do_action() 传递。

function echo_comment_id( $comment_id ) 
{
   echo 'Comment ID ' . $comment_id . ' could not be found';
}
add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );

注释

要找出一个动作的参数的ID和名称,只需搜索匹配 do_action() 调用的代码库。举例来说,如果你挂载到’save_post’,你会在 post.php 找到:

&lt;?php do_action( 'save_post', $post_ID, $post ); ?&gt;

你的 add_action 调用将是这样:

&lt;?php add_action( 'save_post', 'my_save_post', 10, 2 ); ?&gt;

而且你的函数将是这样:

function my_save_post( $post_ID, $post )
{
   // do stuff here
}

在一个类中使用 add_action

当你的插件或主题使用类来创建时,使用 add_action 钩子,在类中添加 $this 和 函数名称 到你的 add_action 回调,像这样:

class MyPluginClass
{
    public function __construct()
    {
         //add your actions to the constructor!
         add_action( 'save_post', array( $this, 'myplugin_save_posts' ) );
    }
 
    public function myplugin_save_posts()
    {
         //do stuff here...
    }
}

函数历史

始见于 WordPress 1.2.0

源文件

add_action() 位于 wp-includes/plugin.php

  • 原文:http://codex.wordpress.org/Function_Reference/add_action
  • 编译:倡萌@WordPress大学

来源:

https://www.wpdaxue.com/add_action.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32277.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?