### 描述

WordPress核心执行期间，在特定点执行时或特定事件发生时，Actions是钩子。插件可以指定使用Action API在这些点上执行其一个或多个PHP函数。

### 参数

**$tag**

(string)(必填)连接到 $function\_to\_add 的操作名称是被挂钩。

**$function\_to\_add**

(callable)(必填)希望调用的函数的名称。

**$priority**

(int)(可选)用于指定与特定操作关联的函数执行的顺序。较低的数字对应于较早的执行，具有相同优先级的函数按照它们被添加到操作中的顺序执行。

默认值：10

**$accepted\_args**

(int)(可选)函数接受的参数个数。

默认值：1

### 返回

(true)将永远返回true。

### 来源

文件：wp-includes/plugin.php

 ```
function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
    return add_filter( $tag, $function_to_add, $priority, $accepted_args );
}
```

### 更多信息

**用法**

 ```
add_action( $hook, $function_to_add, $priority, $accepted_args );
```

要查找操作的参数的数量和名称，只需在代码库中搜索匹配的do\_action()调用。例如，如果您要挂钩’save\_post’，您会在post.php中找到它：

 ```
do_action( 'save_post', $post_ID, $post, $update );
```

add\_action调用如下所示：

 ```
add_action( 'save_post', 'wpdocument_my_save_post', 10, 3 );
```

函数调用是：

 ```
function wpdocument_my_save_post( $post_ID, $post, $update ) {
   // do stuff here
}
```

### 用户贡献的笔记

**与类一起使用**

要在使用类构建插件或主题时使用add\_action()，需要使用数组可调用语法。您可以将函数作为数组传递给add\_action()，第一个元素是$this，然后是类方法的名称，如下所示:

 ```
/**
 * Class WP_Docs_Class.
 */
class WP_Docs_Class {
 
    /**
     * Constructor
     */
    public function __construct() {
        add_action( 'save_post', array( $this, 'wpdocument_save_posts' ) );
    }
 
    /**
     * Handle saving post data.
     */
    public function wpdocument_save_posts() {
        // do stuff here...
    }
}
 
$wpdocumentclass = new WP_Docs_Class();
```

**与类中的静态函数一起使用**

如果类是静态调用的，方法必须如下所示，因为$this不可用。如果类被扩展，也可以这样做。使用以下:

 ```
/**
 * Class WP_Docs_Static_Class.
 */
class WP_Docs_Static_Class {
 
    /**
     * Initializer for setting up action handler
     */
    public static function init() {
        add_action( 'save_post', array( get_called_class(), 'wpdocument_save_posts' ) );
    }
 
    /**
     * Handler for saving post data.
     */
    public static function wpdocument_save_posts() {
        // do stuff here...
    }
}
 
WP_Docs_Static_Class::init();
```

**简单钩子**

每当你的博客上有文章发表时，给一些朋友发邮件:

 ```
/**
 * Send email to my friends.
 *
 * @param int $post_id Post ID.
 * @return int Post ID.
 */
function wpdocument_email_friends( $post_id ) {
    $friends = 'bob@example.org, susie@example.org';
    wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );
 
    return $post_id;
}
add_action( 'publish_post', 'wpdocument_email_friends' );
```

**接受的参数**

如果设置要传递参数，则挂接函数可以选择性地接受来自操作调用的参数。在这个简单的示例中，echo\_comment\_id函数接受$comment\_id参数，当使用comment\_id\_not\_found过滤器钩子运行do\_action()调用时，该参数将自动传递给该参数。

 ```
/**
 * Warn about comment not found
 *
 * @param int $comment_id Comment ID.
 */
function echo_comment_id( $comment_id ) {
    printf( 'Comment ID %s could not be found', esc_html( $comment_id ) );
}
add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );
```

**在类中使用时传递参数**

要在用add\_action调用类中的方法时将参数传递给它，可以执行以下操作:

 ```
public function __construct() {
    // Actions
    add_action('init', array($this, 'call_somefunction'));
}
 
/**
 *    Intermediate function to call add_action with parameters
 */
public function call_somefunction() {
    $this->somefunction('Hello World');
}
 
/**
 *    Actual function that does something
 */
public function somefunction($text) {
    echo $text;
}
```