Inside hook_civicrm_post, when I comment the below line

$user_id = wp_update_user( array( 'ID' => $user_id,'user_email' => $objectRef[Email1]));

It does not give "Could not find valid key" error but when I uncomment the above line it gives the error

Kindly let me know how I can fix this

$user_id  = $uf_match['values'][$uf_match['id']]['uf_id'];
        $data = get_userdata($user_id);
        if ($data->user_mail != $objectRef[Email1]){
         //$user_id = wp_update_user( array( 'ID' => $user_id,'user_email' => $objectRef[Email1]));
  • What kind of entity are you working on in the callback to hook_civicrm_post? As I mention in a comment on Jon's answer, there a number of complications related to updates of the Contact's email address. Sep 14, 2018 at 13:24

1 Answer 1


I'm no WordPress expert, but my reading of the codex is that wp_update_user() will remove cookies for the logged-in user. The docs say it "Uses: wp_clear_auth_cookie()".

I would try to do the same with wp_insert_user() instead, it says in the codex that this can update OR create new users.

  • I have used update_user_meta instead and that seems to work update_user_meta($user_id, 'user_email', $objectRef[Email1]);
    – harshal
    Sep 14, 2018 at 8:06
  • The answer is not quite correct: wp_update_user() only refreshes the auth cookie if the password has been changed for the currently logged-in user. FWIW, CiviCRM itself uses wp_update_user() to update the email address of a synced contact: github.com/civicrm/civicrm-core/blob/master/CRM/Utils/System/… Sep 14, 2018 at 13:06

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.