0

I have some code in my functions.php which you can see below. When I hook in using the action the function doesn't execute, but when I hook into the filter it does, could someone explain why and whats the best practice?

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');
Brad Fletcher
  • 3,547
  • 3
  • 27
  • 42

1 Answers1

0

Both functions have different functionalities. Actions are event based. suppose you want to call a function after submitting a form or page load in that case you will use add_action function.

whereas Filters are used to change the current flow. like a page have content "hello this my test content", and you want to show "hello world this is yours test content" to do so you will use filters.

for further details you can see following links:

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

Community
  • 1
  • 1
Basit Munir
  • 428
  • 4
  • 18