【问题标题】:add_filter is running before add_action In Wordpressadd_filter 在 Wordpress 中的 add_action 之前运行
【发布时间】:2021-06-08 16:55:01
【问题描述】:

实际上我正在尝试添加一些操作add_actionadd_filter。但我不知道为什么add_filteradd_action 之前运行。因为我设置了相同的优先级并且我在 Wordpress 文档中读到,如果操作具有相同的优先级,那么首先编写的操作总是首先运行。

但我不知道 add_filteradd_action 之前运行。

这是我的代码:

上面写在我的代码中的add_action代码。

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 代码写在我的代码下面

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;
}

我想先运行add_action,然后运行add_filter。我该怎么做?

谢谢。

【问题讨论】:

    标签: php wordpress wordpress-action


    【解决方案1】:

    操作和过滤器是fundamentally the same thing,只是一种模式,有时使用操作,有时使用过滤器。基本上,操作允许您“做某事”,过滤器允许您“更改某些内容”。你可能已经知道了,但我只是想清楚一点。通常它们被称为钩子,这就是我在这里引用它们的方式。

    在您的代码示例中,您有两个挂钩,一个称为leaky_paywall_form_processing,一个称为leaky_paywall_mailchimp_merge_fields。因为这是两个不同的钩子,所以它们之间的优先级无关紧要。

    为了更好地解释我要去reference the exact plugin你正在工作。从该文件的第 160 行开始,您会看到:

        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']);
    

    该代码块首先调用钩子leaky_paywall_after_free_user_created,然后调用leaky_paywall_form_processing,始终按此顺序。不管你写什么代码,它总是按那个顺序排列(当然,除非他们改变了他们的代码)。

    如果您想为 same 钩子调用两个不同的函数,那就是优先级的来源。

    不幸的是,该插件的 MailChimp 扩展似乎是付费的,所以我看不到源代码,但最终归结为插件本身来确定调用挂钩的顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 2014-06-04
      • 2016-12-12
      • 1970-01-01
      相关资源
      最近更新 更多