WordPress如何理解如何处理$wp_Filter数组中的(All)键?

时间:2016-12-25 作者:Mohamed Omar

isset( $wp_filter[\'all]), 这个all 钥匙将为所有其他挂钩点火。我需要知道Wordpress如何理解all 已使用。。应该在Wordpress能够理解的地方定义它。

1 个回复
最合适的回答,由SO网友:prosti 整理而成

你有all 例如,在此函数中解释

File: wp-includes/plugin.php
402: /**
403:  * Execute functions hooked on a specific action hook.
404:  *
405:  * This function invokes all functions attached to action hook `$tag`. It is
406:  * possible to create new action hooks by simply calling this function,
407:  * specifying the name of the new hook using the `$tag` parameter.
408:  *
409:  * You can pass extra arguments to the hooks, much like you can with apply_filters().
410:  *
411:  * @since 1.2.0
412:  *
413:  * @global array $wp_filter         Stores all of the filters
414:  * @global array $wp_actions        Increments the amount of times action was triggered.
415:  * @global array $wp_current_filter Stores the list of current filters with the current one last
416:  *
417:  * @param string $tag     The name of the action to be executed.
418:  * @param mixed  $arg,... Optional. Additional arguments which are passed on to the
419:  *                        functions hooked to the action. Default empty.
420:  */
421: function do_action($tag, $arg = \'\') {
422:    global $wp_filter, $wp_actions, $wp_current_filter;
423: 
424:    if ( ! isset($wp_actions[$tag]) )
425:        $wp_actions[$tag] = 1;
426:    else
427:        ++$wp_actions[$tag];
428: 
429:    // Do \'all\' actions first
430:    if ( isset($wp_filter[\'all\']) ) {
431:        $wp_current_filter[] = $tag;
432:        $all_args = func_get_args();
433:        _wp_call_all_hook($all_args);
434:    }
435: 
436:    if ( !isset($wp_filter[$tag]) ) {
437:        if ( isset($wp_filter[\'all\']) )
438:            array_pop($wp_current_filter);
439:        return;
440:    }
441: 
442:    if ( !isset($wp_filter[\'all\']) )
443:        $wp_current_filter[] = $tag;
444: 
445:    $args = array();
446:    if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
447:        $args[] =& $arg[0];
448:    else
449:        $args[] = $arg;
450:    for ( $a = 2, $num = func_num_args(); $a < $num; $a++ )
451:        $args[] = func_get_arg($a);
452: 
453:    $wp_filter[ $tag ]->do_action( $args );
454: 
455:    array_pop($wp_current_filter);
456: }
该部分:

429:    // Do \'all\' actions first
说话all 具有特殊处理内钩生成器功能。

您将在其他挂钩生成器功能中找到类似的逻辑:

apply_filters
apply_filters_ref_array
apply_filters_deprecated
do_action
do_action_ref_array
do_action_deprecated
所以all 有特殊待遇。

您可以使用all 用于内省,如here 其中,我按顺序列出了单个WordPress请求的所有操作挂钩。

从评论中:我理解,但我想知道WordPress如何理解,当传递这个键时,它会影响所有操作或过滤器。应该在某个地方定义

我花了一些时间才明白你的意思。对有一个叫做$wp_filter 所有挂钩都保存在那里。在示例中add_action( \'all\', \'_20161224_printer\' ); here, 这将在全球范围内。

[all] => WP_Hook Object
        (
            [callbacks] => Array
                (
                    [10] => Array
                        (
                            [_20161224_printer] => Array
                                (
                                    [function] => _20161224_printer
                                    [accepted_args] => 1
                                )

                        )

                )

            [iterations:WP_Hook:private] => Array
                (
                )

            [current_priority:WP_Hook:private] => Array
                (
                )

            [nesting_level:WP_Hook:private] => 0
            [doing_action:WP_Hook:private] => 
        )

相关推荐

OOP development and hooks

我目前正在为Wordpress编写我的第一个OOP插件。为了帮助我找到一点结构,a boiler plate 这为我奠定了基础。在里面Main.php 有一种方法可以为管理员加载JS和CSS资产:/** * Register all of the hooks related to the admin area functionality * of the plugin. * * @since 0.1.0 * @access private