php add action,WordPress里的add_action函数

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内找到它:

你的add_action 调用看起来会是这样的:

你的函数将会是:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值