Answers.Body: <p>I'm no WordPress expert, but my reading of the codex is that <code>wp_update_user()</code> will remove cookies for the logged-in user. The docs say it "Uses: <code>wp_clear_auth_cookie()</code>".  </p>
  
  <p>I would try to do the same with <a href="https://codex.wordpress.org/Function_Reference/wp_insert_user" rel="nofollow noreferrer">wp_insert_user()</a> instead, it says in the codex that this can update OR create new users.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-14T08:06:36.200Z
Answers.Text: I have used update_user_meta instead and that seems to work  update_user_meta($user_id, 'user_email', $objectRef[Email1]);
Answers.UserDisplayName: harshal
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-14T13:06:50.763Z
Answers.Text: 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: https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/System/WordPress.php#L580
Answers.UserDisplayName: Christian Wach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-14T05:42:16.403Z
Answers.LastActivityDate: 2018-09-14T05:42:16.403Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>Inside hook_civicrm_post, when I comment 
  the below line </p>
  
  <blockquote>
    <p>$user_id = wp_update_user( array( 'ID' => $user_id,'user_email' =>
    $objectRef[Email1]));</p>
  </blockquote>
  
  <p>It does not give "Could not find valid key" error 
  but when I uncomment the above line it gives the error</p>
  
  <p>Kindly let me know how I can fix this </p>
  
  <pre><code>$user_id  = $uf_match['values'][$uf_match['id']]['uf_id'];
          $data = get_userdata($user_id);
          if ($data-&gt;user_mail != $objectRef[Email1]){
           //$user_id = wp_update_user( array( 'ID' =&gt; $user_id,'user_email' =&gt; $objectRef[Email1]));
          }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-09-14T13:24:47.203Z
Comments.Text: 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.
Comments.UserDisplayName: Christian Wach
Tags: <wordpress>
Title: Error Could not find valid session key wordpress
ThreadText: # Error Could not find valid session key wordpress
  
  (Asked by: username_0 on 2018-09-13)
  
  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]));
          }
  
  ```
  
  **Tags:** wordpress
  
  > **Comments:**
  > **username_1**: 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-09-14. Score: 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()](https://codex.wordpress.org/Function_Reference/wp_insert_user) instead, it says in the codex that this can update OR create new users.
  
  > **Comments:**
  > **username_0**: I have used update_user_meta instead and that seems to work  update_user_meta($user_id, 'user_email', $objectRef[Email1]);  
  > **username_1**: 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: https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/System/WordPress.php#L580  
  
  ---
  
  