簡體   English   中英

WordPress do_action功能幫助

[英]WordPress do_action function assistance

我正在嘗試將自己介紹給Wordpress,在我安裝主題並進入其中進行一些編輯后,我發現了這個調用:

\\可濕性粉劑內容\\主題\\追趕珠峰\\ footer.php

do_action( 'catcheverest_site_generator' );

我跟蹤它到這個功能,我完全迷失了:

function do_action($tag, $arg = '') {
    global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;

    if ( ! isset($wp_actions) )
        $wp_actions = array();

    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; $a < func_num_args(); $a++ )
        $args[] = func_get_arg($a);

    // Sort
    if ( !isset( $merged_filters[ $tag ] ) ) {
        ksort($wp_filter[$tag]);
        $merged_filters[ $tag ] = true;
    }

    reset( $wp_filter[ $tag ] );

    do {
        foreach ( (array) current($wp_filter[$tag]) as $the_ )
            if ( !is_null($the_['function']) )
                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));

    } while ( next($wp_filter[$tag]) !== false );

    array_pop($wp_current_filter);
}

你能描述上面案例中的數據流嗎?

源代碼看起來相當復雜,但從實用的角度來看,知道do_action創建一個鈎子就足夠了,它允許你通過掛鈎函數(使用add_action )在特定位置執行代碼。 在您的情況下,主題作者允許您在頁腳中執行代碼。 您可以在functions.php文件或插件中放置以下函數。 它將在主題的頁腳中回顯“Hello world”。

function my_site_generator(){
    echo 'Hello world';
}
add_action( 'catcheverest_site_generator', 'my_site_generator' );

這是一個非常廣泛的問題,超出了Stack Overflow的范圍。

動作和過濾器,或鈎子,是“Wordpress”的做事方式。 Wordpress頁面上有一個循環,按優先級執行操作,然后返回在將其應用到頁面之前將過濾的內容。

有關於動作和過濾器的完整書籍,你一定要查看一些Wordpress插件書籍以獲取更多詳細信息。

這本書幫助我了解了我學習時發生的事情:

http://www.amazon.com/Professional-WordPress-Plugin-Development-Williams/dp/0470916222

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM