This page redirects to an external site: https://developer.wordpress.org/reference/functions/add_action/
Languages: English • 日本語 한국어 • 中文(简体) • (Add your language)
특별한 action 에 함수를 hook(특별한 위치에서 함수 실행을 추가하거나 실행되지 않도록 제거합니다).
Action hook 목록: Plugin API/Action Reference Action은 일반적으로 do_action()에 의해 실행됩니다.
<?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?>
블로그에 포스트를 게시할 때마다 친구의 이메일로 발송하는 예:
function email_friends( $post_ID )
{
$friends = '[email protected], [email protected]';
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');
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).
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, 2 ); ?>
And your function would be:
function my_save_post( $post_ID, $post )
{
// do stuff here
}
Since: 1.2.0
add_action() is located in wp-includes/plugin.php.