常用函数-add_action()
说明
将函数连接到指定action(动作)。
在Plugin API/Action Reference 上查看动作hook列表。wordpress核心调用do_action() 时触发动作。
用法
<?php add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?>
示例
博客发表新内容时用电子邮件通知朋友:
1 2 3 4 5 6 7 |
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'); |
参数
$tag
(字符串)希望连接到的动作名称(在Plugin API/Action Reference 上查看动作hook列表)
$function_to_add
(回调)希望调用的函数名称。注意: the PHP documentation for the ‘callback’ type中解释的语法均可用。
$priority
函数的重要程度。改变此参数以决定函数与其他函数的调用顺序。默认值为10,因此(例如)将值设为5时函数运行较早,设为12时运行则较晚。
$accepted_args
函数所接受参数的数量。在WordPress 1.5.1及之后版本中,连接的函数可吸收其它在调用do_action() 或 apply_filters()时设置的参数。例如,comment_id_not_found动作将传递任何函数,若该函数将所请求的评论编号连接到该动作。
常用tag
把在Plugin API/Action Reference查到的常用tag贴一下:
前台:
muplugins_loaded
plugins_loaded
load_textdomain
set_current_user
init wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_selection
wp template_redirect
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts
loop_start
the_post
loop_end
get_sidebar
dynamic_sidebar
wp_meta
get_footer
wp_footer
shutdown
后台:
muplugins_loaded
plugins_loaded
load_textdomain
auth_cookie_valid
set_current_user
init
wp_loaded
auth_redirect
admin_menu
admin_init
parse_request
send_headers
wp admin_head
adminmenu
admin_notices
admin_footer
shutdown
更多可到这里查看:http://adambrown.info/p/wp_hooks