2016-11-10 66 views
0

我在我的functions.php中有一些代碼,你可以在下面看到。當我使用該動作掛鉤時,該函數不會執行,但是當我鉤入它的過濾器時,有人可以解釋爲什麼以及最佳實踐是什麼?WordPress的過濾器VS. action

ACTION

// ADD £40 ON SUCCESFUL SUBSCRIPTION PAYMENT (EXAMPLE 1) 
function custom_add_funds($user_id) { 

    // get current user's funds 
    $funds = get_user_meta($user_id, 'account_funds', true); 

    // add £40 
    $funds = $funds + 40; 

    // add funds to user 
    update_user_meta($user_id, 'account_funds', $funds); 

} 
add_action('processed_subscription_payment', 'custom_add_funds'); 

FILTER

// ADD £40 ON SUCCESFUL SUBSCRIPTION PAYMENT (EXAMPLE 2) 
function custom_add_funds_two($user_id) { 

    // get current user's funds 
    $funds = get_user_meta($user_id, 'account_funds', true); 

    // add £40 
    $funds = $funds + 40; 

    // add funds to user 
    update_user_meta($user_id, 'account_funds', $funds); 

} 
add_filter('processed_subscription_payment','custom_add_funds_two'); 

回答

0

兩個函數具有不同的功能。 操作是基於事件的。假設你想在提交表單或頁面加載後調用一個函數,那麼你將使用add_action函數。

過濾器用於改變當前流量。就像一個頁面有內容「你好,我的測試內容」,並且你想顯示「你好世界這是你的測試內容」這樣做你會使用過濾器。

進一步的細節,你可以看到以下鏈接:

Difference between add_filter versus add_action https://wordpress.stackexchange.com/questions/120339/difference-between-do-action-and-add-action