WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

ko:Function Reference/add action

Description

특별한 action 에 함수를 hook(특별한 위치에서 함수 실행을 추가하거나 실행되지 않도록 제거합니다).

Action hook 목록: Plugin API/Action Reference Action은 일반적으로 do_action()에 의해 실행됩니다.

Usage

 <?php add_action$tag$function_to_add$priority$accepted_args ); ?> 

Parameters

$tag
(string) (required) 연결하고자 하는 hook 이름 (Action hook 리스트 :Plugin API/Action Reference ). 또는 테마나 플러그인에 포함된 action을 넣을 수 있습니다.
Default: None
$function_to_add
(callback) (required) 호출하고자 하는 함수의 이름입니다. Note: Only string-formatted syntaxes listed in the PHP documentation for the 'callback' type are valid.
Default: None
$priority
(int) (optional) 호출할 함수의 중요도입니다. 다른 함수의 앞 또는 뒤에 호출하기 위해 이 값을 변경합니다. 기본값:10, 예를 들어 5로 설정하면 이전에 실행 되고 12을 설정하면 나중에 실행됩니다.
Default: 10
$accepted_args
(int) (optional) 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.
Default: 1

Examples

Simple Hook

블로그에 포스트를 게시할 때마다 친구의 이메일로 발송하는 예:

function 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', 'email_friends');

Take Arguments

The hooked function takes one argument from the action. Specifically, the 'echo_comment_id' function, takes the argument $comment_ID. It then echos the value of the received argument.

function echo_comment_id( $comment_ID ) 
{
   echo "I just received $comment_ID";
}
add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );


You can also pass the callback parameter as a anonymous function, for example:  <?php add_action('wp_head', function() { echo 'something';}) ?> 


Important note: It's not possible to remove an anonymous function using 'remove_action' (needs a function name as second argument).

Notes

To find out the number and name of arguments for an action, simply search the code base for the matching do_action() call. For example, if you are hooking into 'save_post', you would find it in post.php:  <?php do_action'save_post'$post_ID$post ); ?> 

Your add_action call would look like:  <?php add_action'save_post''my_save_post'10); ?> 

And your function would be:

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

Change Log

Since: 1.2.0

Source File

add_action() is located in wp-includes/plugin.php.

Related

Actions: has_action(), add_action(), do_action(), do_action_ref_array(), did_action(), remove_action(), remove_all_actions()

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.