1

The hook

 profile_update

gets fired in case the user updates his profile, that is also when he reset the first time his password after registration and when he register.

How do I distinguish between the three cases?

EDIT: There are, at least, 3 cases to distinguish, in which that hook is called.

  1. user's first registration step
    • The user fills email and username and save (profile_update gets called here), being presented the request to check email for the verification process
  2. user reset password after registration
    • The user checks his mail, follow the suggested url, reset the password and save (profile_update gets called here)
  3. user updates his profile
    • The user log in and update some data within his profile and save (profile_update gets called here)

I think it is possible to distinguish case 3, verifying if someone his currently logged in calling

is_user_logged_in()

Still there is the problem to distinguish case 2 from 1. As s_ha_dum points out there is the possibility to check the user_activation_key. If the key is created not at the time 1 but a time 2, the 3 cases are distinguishable. (Even if I personally believe the hook covers too much cases and shouldn't).

2 Answers 2

4

Technically, you can't. That filter doesn't pass in any data that will specifically allow you to distinguish between those two cases.

2086                    /**
2087                     * Fires immediately after an existing user is updated.
2088                     *
2089                     * @since 2.0.0
2090                     *
2091                     * @param int    $user_id       User ID.
2092                     * @param object $old_user_data Object containing user's data prior to update.
2093                     */
2094                    do_action( 'profile_update', $user_id, $old_user_data );

You notice that the filter does pass through $old_user_data so you could check the user_activation_key in the *_users table. That should get you close, however, I think that that field is populated when a user resets the password, and not just when the account is created.

To be absolutely sure you'd probably need to set a user meta value on first login then check that for subsequent logins.

1
  • This is a very very good point. I edited the question, to improve it, for it was put in a bad way. Can i ask you to check edits and tell me if you think i'm right? I'll check it by code asap. Commented Dec 7, 2015 at 19:08
1

In order, to distinguish case 1 and 3 i actually used:

add_action( 'profile_update', 'when_profile_update', 10, 2 );
function when_profile_update( $user_id, $old_user_data ) {
    if (is_user_logged_in()) { 
        // Updating profile info when logged in 
    }else{ 
        if (empty($old_user_data->user_activation_key)) { 
              // Registering
        }
    }
}

At this time, in my site profile_update is not fired for password reset, so case 2 is not happening.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.