2016-12-20 32 views
2

這裏是WordPress中的簡單過濾功能。
我已經理解了這段代碼的主要過程,但有一點不明確。 我沒有通過$content參數add_filter函數,但它從哪裏來的?動作或過濾器參數來自哪裏?

如果WordPress支持默認的參數,它的確定又如何知道什麼參數是可以爲特定的過濾器或操作事件?

<?php 
    add_filter('the_content', 'prowp_profanity_filter'); 
    function prowp_profanity_filter($content) { 
    $profanities = array('sissy', 'dummy'); 
    $content = str_ireplace($profanities, '[censored]', $content); 
    return $content; 
} 
?> 

謝謝。

回答

1

the_content濾波器鉤位於其內部代碼在wp-includes/post-template.php核心文件定義the_content()函數(開始於線222)

/** 
* Display the post content. 
* 
* @since 0.71 
* 
* @param string $more_link_text Optional. Content for when there is more text. 
* @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. 
*/ 
function the_content($more_link_text = null, $strip_teaser = false) { 
    $content = get_the_content($more_link_text, $strip_teaser); 
    /** 
    * Filters the post content. 
    * 
    * @since 0.71 
    * 
    * @param string $content Content of the current post. 
    */ 
    $content = apply_filters('the_content', $content); 
    $content = str_replace(']]>', ']]&gt;', $content); 
    echo $content; 
} 

如果看看到代碼你會了解$content在使用過濾器鉤子時使用的參數也被用作該函數中的變量來處理通過它的數據,然後輸出它。

每個動作和過濾鉤子在覈心代碼文件或模板定義自己的參數,因爲它們改變默認的行爲方式,在不改變該核心文件或模板的源代碼。

我希望這回答你的問題。

在互聯網上搜索,你會很容易找到所有現有的過濾器鉤子和動作鉤子及其各自參數的列表。

0

LoïcTheAztec是正確的,我只是想補充的是$content當過濾器的功能(the_content)被觸發自動填充。

apply_filters允許被添加,並傳遞到鉤子附加參數。你會發現更多的細節here