WordPress 功能函数—— add_action(将函数挂接到特定的操作上)

描述

WordPress核心执行期间,在特定点执行时或特定事件发生时,Actions是钩子。插件可以指定使用Action API在这些点上执行其一个或多个PHP函数。

参数

$tag

(string)(必填)连接到 $function_to_add 的操作名称是被挂钩。

$function_to_add

(callable)(必填)希望调用的函数的名称。

$priority

(int)(可选)用于指定与特定操作关联的函数执行的顺序。较低的数字对应于较早的执行,具有相同优先级的函数按照它们被添加到操作中的顺序执行。

默认值:10

$accepted_args

(int)(可选)函数接受的参数个数。

默认值:1

返回

(true)将永远返回true。

来源

文件: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 );
}

更多信息

用法

add_action( $hook, $function_to_add, $priority, $accepted_args );

要查找操作的参数的数量和名称,只需在代码库中搜索匹配的do_action()调用。例如,如果您要挂钩'save_post',您会在post.php中找到它:

do_action( 'save_post', $post_ID, $post, $update );

add_action调用如下所示:

add_action( 'save_post', 'wpdocument_my_save_post', 10, 3 );

函数调用是:

function wpdocument_my_save_post( $post_ID, $post, $update ) {
   // do stuff here
}

用户贡献的笔记

与类一起使用

要在使用类构建插件或主题时使用add_action(),需要使用数组可调用语法。您可以将函数作为数组传递给add_action(),第一个元素是$this,然后是类方法的名称,如下所示:

/**
 * Class WP_Docs_Class.
 */
class WP_Docs_Class {
 
    /**
     * Constructor
     */
    public function __construct() {
        add_action( 'save_post', array( $this, 'wpdocument_save_posts' ) );
    }
 
    /**
     * Handle saving post data.
     */
    public function wpdocument_save_posts() {
        // do stuff here...
    }
}
 
$wpdocumentclass = new WP_Docs_Class();

与类中的静态函数一起使用

如果类是静态调用的,方法必须如下所示,因为$this不可用。如果类被扩展,也可以这样做。使用以下:

/**
 * 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(), 'wpdocument_save_posts' ) );
    }
 
    /**
     * Handler for saving post data.
     */
    public static function wpdocument_save_posts() {
        // do stuff here...
    }
}
 
WP_Docs_Static_Class::init();

简单钩子

每当你的博客上有文章发表时,给一些朋友发邮件:

/**
 * Send email to my friends.
 *
 * @param int $post_id Post ID.
 * @return int Post ID.
 */
function wpdocument_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', 'wpdocument_email_friends' );

接受的参数

如果设置要传递参数,则挂接函数可以选择性地接受来自操作调用的参数。在这个简单的示例中,echo_comment_id函数接受$comment_id参数,当使用comment_id_not_found过滤器钩子运行do_action()调用时,该参数将自动传递给该参数。

/**
 * 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 );

在类中使用时传递参数

要在用add_action调用类中的方法时将参数传递给它,可以执行以下操作:

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;
}
本文原创,作者:萨龙龙,其版权均为4166am金沙信心之选所有。
如需转载,请注明出处:/wordpress-functions-add-action.html



WordPress\u6838\u5fc3\u6267\u884c\u671f\u95f4\uff0c\u5728\u7279\u5b9a\u70b9\u6267\u884c\u65f6\u6216\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0cActions\u662f\u94a9\u5b50\u3002\u63d2\u4ef6\u53ef\u4ee5\u6307\u5b9a\u4f7f\u7528ActionAPI\u5728\u8fd9\u4e9b\u70b9\u4e0a\u6267\u884c\u5176\u4e00\u4e2a\u6216\u591a\u4e2aPHP\u51fd\u6570\u3002



\u53c2\u6570



$tag



(string)\u2026@\u8428\u9f99\u7f51\u7edc&appkey=746468751&pic=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&searchPic=true","qq":"https:\/\/connect.qq.com\/widget\/shareqq\/index.html?url=https:\/\/salongweb.com\/wordpress-functions-add-action.html&title=WordPress \u529f\u80fd\u51fd\u6570\u2014\u2014 add_action(\u5c06\u51fd\u6570\u6302\u63a5\u5230\u7279\u5b9a\u7684\u64cd\u4f5c\u4e0a)&pics=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&summary=\u63cf\u8ff0



WordPress\u6838\u5fc3\u6267\u884c\u671f\u95f4\uff0c\u5728\u7279\u5b9a\u70b9\u6267\u884c\u65f6\u6216\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0cActions\u662f\u94a9\u5b50\u3002\u63d2\u4ef6\u53ef\u4ee5\u6307\u5b9a\u4f7f\u7528ActionAPI\u5728\u8fd9\u4e9b\u70b9\u4e0a\u6267\u884c\u5176\u4e00\u4e2a\u6216\u591a\u4e2aPHP\u51fd\u6570\u3002



\u53c2\u6570



$tag



(string)\u2026&site=\u8428\u9f99\u7f51\u7edc","zone":"https:\/\/sns.qzone.qq.com\/cgi-bin\/qzshare\/cgi_qzshare_onekey?url=https:\/\/salongweb.com\/wordpress-functions-add-action.html&title=WordPress \u529f\u80fd\u51fd\u6570\u2014\u2014 add_action(\u5c06\u51fd\u6570\u6302\u63a5\u5230\u7279\u5b9a\u7684\u64cd\u4f5c\u4e0a)&desc=\u63cf\u8ff0



WordPress\u6838\u5fc3\u6267\u884c\u671f\u95f4\uff0c\u5728\u7279\u5b9a\u70b9\u6267\u884c\u65f6\u6216\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0cActions\u662f\u94a9\u5b50\u3002\u63d2\u4ef6\u53ef\u4ee5\u6307\u5b9a\u4f7f\u7528ActionAPI\u5728\u8fd9\u4e9b\u70b9\u4e0a\u6267\u884c\u5176\u4e00\u4e2a\u6216\u591a\u4e2aPHP\u51fd\u6570\u3002



\u53c2\u6570



$tag



(string)\u2026&summary=&site=\u8428\u9f99\u7f51\u7edc"}>



WordPress\u6838\u5fc3\u6267\u884c\u671f\u95f4\uff0c\u5728\u7279\u5b9a\u70b9\u6267\u884c\u65f6\u6216\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\uff0cActions\u662f\u94a9\u5b50\u3002\u63d2\u4ef6\u53ef\u4ee5\u6307\u5b9a\u4f7f\u7528A\u2026","thumb":"https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg","date":{"day":"03","year":"2019\/04"},"desc":"WordPress\u4e3b\u9898|WordPress \u4f01\u4e1a\u4e3b\u9898|WordPress \u5546\u57ce\u4e3b\u9898|\u9ad8\u54c1\u8d28\u7f51\u7ad9\u5b9a\u5236","style":{"light":{"logo":"https:\/\/pic.salongweb.com\/2022\/12\/logo-light.png","title":"#242F40","text":"#5B6B82","meta":"#9AA7B9","border":"#EFF5FF","bg":"#fff"},"dark":{"logo":"https:\/\/pic.salongweb.com\/2022\/12\/logo-dark.png","title":"#B1C1E8","text":"#b3bdd3","meta":"#565E75","border":"#1C2032","bg":"#171B2C"}}} :loading="posterLoading">