0

I want an Ultimate Member action to be performed when I assign a particular role to a user. A role "old_member" exists. If I assign this role to a user, the account should deactivate itself with the Ultimate Member action "deactivate()". Unfortunately, it does not work with this code:

    add_action( 'set_user_role', 'user_role_update', 10, 2);
    function user_role_update( $user_id, $new_role ) {
    if ($new_role == 'old_member') {
        global $ultimatemember;
        um_fetch_user( $user_id );
        $ultimatemember->user->deactivate();
    }
}

Here you can see the code of ultimate member: https://github.com/ultimatemember/ultimatemember/blob/master/includes/admin/core/class-admin-users.php#L97

Can someone tell me what the mistake is?

  • But you're not using the code you've linked to? Have you tried UM()->user()->deactivate()? (It looks like you got your code from this example, but that's the only mention of $ultimatemember in the source, and even the @usage block there has the UM()->user()->deactivate() form.) – Rup May 14 at 22:11
  • And IMO mixing the um_fetch_user call (not superficially using the UM object) and the UM()->user() call, not obviously the same objects, is silly too. But it looks like that's how they're expecting you to use this interface. Thankfully um_fetch_user(123) becomes UM()->user()->set(123) under the covers anyway. – Rup May 14 at 22:15
  • Thank you for your answer. Yes I also tried UM()->user()->deactivate() but its not working. I have now found out that the update_user_meta method makes it easier. But it does not work that way either. Somewhere lies the error. This is how the code would look like (I let the if function out for now): function user_status_update( $user_id, $role ) { update_user_meta( $user_id, 'account_status', 'inactive'); } do_action( 'set_user_role', 'user_status_update', 10, 2); – anilgky May 15 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.