简体   繁体   English

在代码中更新用户角色时触发 WordPress 挂钩

[英]Firing WordPress hook when user role is updated in code

I am trying to fire an event every time a users role has changed.每次用户角色发生变化时,我都会尝试触发一个事件。 So far I got this:到目前为止,我得到了这个:

add_action( 'set_user_role', function( $user_id, $role, $old_roles )
  {
    error_log("usu fired2!");

    $user_info = get_userdata($user_id);
    $email = $user_info->user_email;

    error_log($email);
    error_log(print_r($old_roles, true));
    error_log($role);

    if ($role == "subscriber") {
      MC_user_tag_l2s($email, "klubmedlem", "active");
    } else {
      MC_user_tag_l2s($email, "klubmedlem", "inactive");
    }

  }, 10, 3 );

It works very well when I am updating the users role manually on the edit-profile page.当我在编辑配置文件页面上手动更新用户角色时,它工作得很好。 However, it is not working when the users role is updated through code.但是,当通过代码更新用户角色时,它不起作用。 Ie when a user creates a subscription with WooCommerce subscriptions.即当用户使用 WooCommerce 订阅创建订阅时。

Any ideas as to which hook I should use instead?关于我应该使用哪个钩子的任何想法?

It seems that you want to use the action woocommerce_subscriptions_updated_users_role看来您想使用操作woocommerce_subscriptions_updated_users_role

Description: When a subscription is activated, suspended, cancelled or expired, the user's role will also be changed, which triggers the 'woocommerce_subscriptions_updated_users_role' hook.描述:当订阅被激活、暂停、取消或过期时,用户的角色也会发生变化,这会触发 'woocommerce_subscriptions_updated_users_role' 钩子。

Source: https://docs.woocommerce.com/document/subscriptions/develop/action-reference/资料来源: https://docs.woocommerce.com/document/subscriptions/develop/action-reference/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM