首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]do_action()

发布于 2024-06-06 21:50:44
0
425

do_action() 是WordPress的核心函数,该函数没有返回值。作用是创建一个钩子,在特定的地方执行插件或者主题开发者挂载的函数,一般存在于某个特殊的节点或者事件上。do_action() ...

do_action() 是WordPress的核心函数,该函数没有返回值。作用是创建一个钩子,在特定的地方执行插件或者主题开发者挂载的函数,一般存在于某个特殊的节点或者事件上。do_action() 的调用方法如下:


do_action( string $tag,  $arg = '' )

$tag:必填(字符串)。将要被指向动作的函数名称。
$arg:选填(混合型)。传递给钩子函数的其他参数。 默认为空。
该函数定义在 wp-includes/plugin.php 文件中:


function do_action($tag, $arg = '') {
    global $wp_filter, $wp_actions, $wp_current_filter;
    if ( ! isset($wp_actions[$tag]) )
        $wp_actions[$tag] = 1;
    else
        ++$wp_actions[$tag];
    // Do 'all' actions first
    if ( isset($wp_filter['all']) ) {
        $wp_current_filter[] = $tag;
        $all_args = func_get_args();
        _wp_call_all_hook($all_args);
    }
    if ( !isset($wp_filter[$tag]) ) {
        if ( isset($wp_filter['all']) )
            array_pop($wp_current_filter);
        return;
    }
    if ( !isset($wp_filter['all']) )
        $wp_current_filter[] = $tag;
    $args = array();
    if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
        $args[] =& $arg[0];
    else
        $args[] = $arg;
    for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
        $args[] = func_get_arg($a);
    $wp_filter[ $tag ]->do_action( $args );
    array_pop($wp_current_filter);
}
评论
一个月内的热帖推荐
道亮_(:з」∠)_
Lv.1管理员

404

帖子

30

小组

2177

积分

赞助商广告
站长交流