经
常往functions.php里面丢东西,这似乎是定义一个新函数时必须用到的添加动作的wordpress函数。
今天就把它搞清楚吧。因为前几天冒出了学习用bootstrap弄一个wp主题的念头,但发现还是不靠谱,
很多函数和常量还不熟悉,拿一个主题来解读也分不清哪些函数是自带的,哪些是自定义的,所以工作量巨大。
当然,还是要慢慢来。因为我正试着能不能培养第二、甚至第三兴趣出来,只要有时间坚持,什么事都不怕做不好。
add_action()和add_filter()功能上没有太大区别。这东西还得去查wordpress官方文档,虽说是中文文档,但大部分还是英文说明的。官方文档如下:我翻译了一些,仅供参考。
Description 描述
Hooks a function on to a specific action.
See Plugin API/Action Reference for a list of action hooks. Actions are (usually) triggered when the WordPress core calls do_action().
Usage 用法
add_action( $tag, $function_to_add, $priority, $accepted_args );
Parameters 参数
$tag
(string) (required) The name of the action to which $function_to_add is hooked. (See Plugin API/Action Reference for a list of action hooks). Can also be the name of an action inside a theme or plugin file, or the special tag "all", in which case the function will be called for all hooks.Default: None
$function_to_add
(callback) (required) The name of the function you wish to be hooked. Note: Only string-formatted syntaxes listed in the PHP documentation for the 'callback' type are valid.Default: None
$priority
(int) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.Default: 10
$accepted_args
(int) (optional) The number of arguments the hooked function accepts. 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-简单的钩子】每当你发表一篇文章时用Email通知你的朋友:
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' );【Accepted Arguments-被接受的参数】
如果任何被设置为传递,一个挂钩函数可以有选择地接受来自操作调用的参数。在这个简单的例子中,使用comment_id_not_found过滤钩子运行时,echo_comment_id函数将$COMMENT_ID参数自动传递给do_action()调用。function echo_comment_id( $comment_id ) { echo 'Comment ID ' . $comment_id . ' could not be found'; } add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );【Notes-注释】
为了找出一个动作的ID和参数,需要简单搜索匹配do_action()调用要求的代码库,例如:若你需要挂载进入'save_post',你将在post.php内找到它:<?php do_action( 'save_post', $post_ID, $post ); ?>你的add_action 调用看起来会是这样的:
<?php add_action( 'save_post', 'my_save_post', 10, 2 ); ?>你的函数将会是:
function my_save_post( $post_ID, $post ) { // do stuff here }【Using add_action with a class-连同一个类一起使用add_action】
当你使用各种类去制作你的插件和主题时采用add_action钩子在类里面把函数名称和$this参数一起添加到你的add_action调用中,像这样:class MyPluginClass { public function __construct() { //add your actions to the constructor! add_action( 'save_post', array( $this, 'myplugin_save_posts' ) ); } public function myplugin_save_posts() { //do stuff here… } }
Source File 源文件
add_action()位于wp-includes/plugin.php
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/3366.html