add_filter is running before add_action In Wordpress

927 Views Asked by At

Actually I am trying to add some actions add_action and add_filter. But I don't know why add_filter is running before add_action. As I have set the same priority & I read in Wordpress documentation that if actions have same priority then the action which is written first always run first.

But I don't know in my case add_filter is running before add_action.

Here is my code:

add_action code which is written above in my code.

add_action('leaky_paywall_form_processing', 'zeen101_custom_registration_fields_save', 10, 5);
function zeen101_custom_registration_fields_save($post_data, $user_id, $price, $mode, $site)
{
    if ($post_data['company']) {
        update_user_meta($user_id, 'company', sanitize_text_field($post_data['company']));
    }
    if ($post_data['city']) {
        update_user_meta($user_id, 'city', sanitize_text_field($post_data['city']));
    }
}

add_filter code which is written below in my code

add_filter('leaky_paywall_mailchimp_merge_fields', 'zeen101_custom_mailchimp_merge_fields', 10, 2);
function zeen101_custom_mailchimp_merge_fields($merge_fields, $email)
{
    $mode = leaky_paywall_get_current_mode();
    $user = get_user_by('email', $email);
    if (!$user) {
        return $merge_fields;
    }

    $level_id = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_level_id', true);
    $levels = leaky_paywall_get_levels();
    $level_name = $levels[$level_id]['label'];

    $created = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_created', true);
    $expires = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_expires', true);
    $firstname = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_first_name', true);
    $lastname = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_last_name', true);
    $email = get_user_meta($user->ID, '_issuem_leaky_paywall_' . $mode . '_email', true);

    $company = get_user_meta($user->ID, 'company', true);
    $city = get_user_meta($user->ID, 'city', true);

    $merge_fields['LP_LEVEL'] = $level_name;
    $merge_fields['LP_SUBDATE'] = $created;
    $merge_fields['LP_EXPIRES'] = $expires;
    $merge_fields['FNAME'] = $firstname;
    $merge_fields['LNAME'] = $lastname;
    $merge_fields['EMAIL'] = $email;
    $merge_fields['COMPANY'] = $company;
    $merge_fields['CITY'] = $city;

    return $merge_fields;
}

I want to run add_action first then add_filter. How I can do that?

Thanks.

1

There are 1 best solutions below

0
On

Actions and filters are fundamentally the same thing, there's just a pattern where sometimes an action is used and sometimes a filter is used. Actions allow you to "do something" and filters allow you to "change something", basically. You might know that already, but I just wanted to be clear on it. Generically they are called hooks which is how I'll refer to them here.

In your code sample, you have two hooks, one called leaky_paywall_form_processing and one called leaky_paywall_mailchimp_merge_fields. Because these are two different hooks, their priority in relation to each other doesn't matter.

In order to better explain that I'm going to reference the exact plugin you are working with. Starting at line 160 in that file you see this:

    if (leaky_paywall_is_free_registration($subscriber_data)) {
        do_action('leaky_paywall_after_free_user_created', $user_id, $_POST);
    }

    do_action('leaky_paywall_form_processing', $_POST, $user_id, $subscriber_data['price'], $mode, $site, $subscriber_data['level_id']);

That code block first calls the hook leaky_paywall_after_free_user_created and then calls leaky_paywall_form_processing, in that order, always. It doesn't matter what code you wrote, it will always be in that order (unless they change their code, of course).

If you had two different functions that you wanted to call for the same hook, that's where priorities come in.

Unfortunately, the MailChimp extension for that plugin appears to be a paid one, so I can't see the source, but ultimately it comes down to the plugin itself to determine the order that hooks get called.