【问题标题】:Wordpress filter Vs. actionWordpress 过滤器对比行动
【发布时间】:2017-03-24 16:07:06
【问题描述】:

我的functions.php 中有一些代码,您可以在下面看到。当我使用该操作挂钩时,该功能不会执行,但是当我挂钩过滤器时它会执行,有人可以解释为什么以及最佳做法是什么?

动作

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

过滤器

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

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    这两个函数都有不同的功能。 动作是基于事件的。假设您想在提交表单或页面加载后调用一个函数,在这种情况下您将使用 add_action 函数。

    过滤器 用于更改当前流。就像一个页面有内容“你好,这是我的测试内容”,并且你想显示“你好 world 这是你的测试内容”,你将使用过滤器。

    更多详情,您可以查看以下链接:

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

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多