WordPress開發(fā)函數(shù)add_action()詳細(xì)介紹
WordPress開發(fā)函數(shù)add_action(),將函數(shù)掛接到特定的操作上。
用法:
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
描述:
操作是wordpress核心在執(zhí)行過程中的特定點(diǎn)或特定事件發(fā)生時(shí)啟動(dòng)的鉤子。插件可以使用Action API指定在這些點(diǎn)上執(zhí)行一個(gè)或多個(gè)PHP函數(shù)。
參數(shù):
$tag
(string) (必需) $function_to_add被鉤住的動(dòng)作的名稱。
$function_to_add
(callable) (必需) 您希望被調(diào)用的函數(shù)的名稱。
$priority
(int) (可選) 用于指定與特定操作相關(guān)聯(lián)的函數(shù)的執(zhí)行順序。較低的數(shù)字對(duì)應(yīng)較早的執(zhí)行,具有相同優(yōu)先級(jí)的函數(shù)按照它們添加到動(dòng)作中的順序執(zhí)行。
默認(rèn)值:10
$accepted_args
(int) (可選) 函數(shù)接受的參數(shù)個(gè)數(shù)。
默認(rèn)值:1
返回:
(true)將總是返回true。
更多的信息:
使用:
add_action( $hook, $function_to_add, $priority, $accepted_args );
要查找操作的參數(shù)數(shù)量和名稱,只需搜索匹配do_action()調(diào)用的代碼基。例如,如果你連接到' save_post ',你會(huì)發(fā)現(xiàn)它在post.php:
do_action( 'save_post', $post_ID, $post, $update );
你的add_action調(diào)用應(yīng)該像這樣:
add_action( 'save_post', 'wpdocs_my_save_post', 10, 3 );
你的函數(shù)是:
function wpdocs_my_save_post( $post_ID, $post, $update ) {
// do stuff here
}
來源:
文件:wp-includes/plugin.php
function add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
return add_filter( $tag, $function_to_add, $priority, $accepted_args );
}
用戶貢獻(xiàn)的筆記:
(由Codex - 6年前貢獻(xiàn))
與類一起使用
要使用add_action()當(dāng)你的插件或主題是使用類構(gòu)建時(shí),你需要使用數(shù)組可調(diào)用語法。你可以將函數(shù)作為數(shù)組傳遞給add_action(), $this作為第一個(gè)元素,然后是類方法的名稱,如下所示:
/**
* Class WP_Docs_Class.
*/
class WP_Docs_Class {
/**
* Constructor
*/
public function __construct() {
add_action( 'save_post', array( $this, 'wpdocs_save_posts' ) );
}
(由Codex - 6年前貢獻(xiàn))
使用類中的靜態(tài)函數(shù)
如果該類是靜態(tài)調(diào)用的,方法必須如下所示,因?yàn)?this不可用。如果類被擴(kuò)展,這也可以工作。使用以下:
/**
* Class WP_Docs_Static_Class.
*/
class WP_Docs_Static_Class {
/**
* Initializer for setting up action handler
*/
public static function init() {
add_action( 'save_post', array( get_called_class(), 'wpdocs_save_posts' ) );
}
(由Codex - 6年前貢獻(xiàn))
簡單的鉤
每當(dāng)博客上有文章發(fā)表時(shí),就給朋友發(fā)郵件:
/**
* Send email to my friends.
*
* @param int $post_id Post ID.
* @return int Post ID.
*/
function wpdocs_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', 'wpdocs_email_friends' );
(安東尼·霍廷4年前貢獻(xiàn))
相關(guān):
do_action()
remove_action()
(由mkormendy - 1年前貢獻(xiàn))
要將變量傳遞給被調(diào)用的action函數(shù),可以在初始編碼的do_action中參數(shù)不可用時(shí)使用閉包(從PHP 5.3+開始)。例如:
add_action('wp_footer', function($arguments) use ($myvar) {
echo $myvar;
}, $priority_integer, $accepted_arguments_integer);
(由lucasbustamante貢獻(xiàn)- 3年前)
在類中使用時(shí)傳遞參數(shù)
在類中通過add_action調(diào)用方法時(shí),要將參數(shù)傳遞給方法,你可以這樣做:
public function __construct() {
// Actions
add_action('init', array($this, 'call_somefunction'));
}
/**
* Intermediate function to call add_action with parameters
*/
public function call_somefunction() {
$this->somefunction('Hello World');
}
/**
* Actual function that does something
*/
public function somefunction($text) {
echo $text;
}
(由Christian Saborio貢獻(xiàn)- 10個(gè)月前)
如何添加一個(gè)動(dòng)作來調(diào)用一個(gè)實(shí)例化類的函數(shù)(帶參數(shù)):
$admin_menu_hider = new AdminMenuHider( UserManagement::get_internal_users() );
add_action(
'wp_before_admin_bar_render',
function () use ( $admin_menu_hider ) {
$admin_menu_hider->change_greeting_message( 'Hello' );
}
);
(由Codex - 6年前貢獻(xiàn))
接受參數(shù)
如果設(shè)置了要傳遞的參數(shù),鉤子函數(shù)可以選擇性地接受來自動(dòng)作調(diào)用的參數(shù)。在這個(gè)簡單的示例中,echo_comment_id函數(shù)接受$comment_id參數(shù),該參數(shù)在使用comment_id_not_found過濾器鉤子運(yùn)行do_action()調(diào)用時(shí)自動(dòng)傳遞給它。
/**
* Warn about comment not found
*
* @param int $comment_id Comment ID.
*/
function echo_comment_id( $comment_id ) {
printf( 'Comment ID %s could not be found', esc_html( $comment_id ) );
}
add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );