WordPress 中文文档

Function Reference/add action

From WordPress Chinese

Jump to: navigation, search

目录

Description

Hooks a function on to a specific action. See Plugin API for a list of hooks for action.

Usage

%%% <?php add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?> %%%

Examples

To email some friends whenever an entry is posted on your blog:

function email_friends($post_ID)  {
   $friends = 'bob@example.org, susie@example.org';
   mail($friends, "sally's blog updated" , 'I just put something on my blog: http://blog.example.com');
   return $post_ID;
}

add_action('publish_post', 'email_friends');

Parameters

$tag 
(string) The name of the action you wish to hook onto.
$function_to_add 
(callback) The name of the function you wish to be called. Note: any of the syntaxes explained in the PHP documentation for the 'callback' type are valid.
$priority 
How important your function is. Alter this to make your function be called before or after other functions. A priority of 1 runs first and a priority of 10 runs last.
$accepted_args 
How many arguments your function takes. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID of the requested comment.Template:Copyedit
用户