title: Wordpress login works partially
body: <p>I try to login the user in wordpress, but it works only for users registered in UI, not for the ones added by program.
  The Login is done as a response to Ajax login request:</p>
  
  <pre><code>//wp_clear_auth_cookie();
  $usr = wp_set_current_user ( $user-&gt;ID, $user-&gt;user_login);
  wp_set_auth_cookie  ( $user-&gt;ID );
  </code></pre>
  
  <p>There is log output for each step, and for any type of user. It is successfully registered in database, loaded from database, and even login is ok. And even a session is created for both kind of users. It can be seen in the database. But after all login flow, when page is redirected or refreshed, only the UI registered users enters the site. The programmatic ones are just not loaded after all successful steps: silently not working, no errors messages, no warnings, no failures. Looks like it needs some additional steps to enable or to activate. All kind of users are shown in dashboard in UI.</p>
  
  <p>I suspect the programmatically added user is not complete or is not activated. This is how the user is registered as response to Ajax registration request:</p>
  
  <pre><code>function register_user($username, $usertype, $externalid)
  {
     $user_email =  'theuseremail@mail.com';
     $result = wp_insert_user(array('user_login' =&gt; $username,
                                    'nice_name'  =&gt; $username,
                                    'user_email' =&gt; $user_email,
                                    'user_pass'  =&gt; '***********'   ));
     $fb_user_id = $result;
     add_user_meta( $fb_user_id, 'specific_attribute', $specific_id, true );
     $user = get_user_by('id', $fb_user_id);
     return $user;
  }
  </code></pre>
  
  <p>The same code is used for both kind of users, the ones registered by UI and the ones registered programatically. There is how the user is loaded from Database as response to Ajax login request:</p>
  
  <pre><code>function load_user($usertype, $specific_id)
  {
     $user = get_users(array(
          'meta_key'     =&gt; 'specific_attribute',
          'meta_value'   =&gt; $specific_id
       ));
     return $user [0];
  }
  </code></pre>
  
  <p>The login uses a metadata field <strong>specific_attribute</strong> in both cases. For users registered by UI this attribute is added manually in the database, because there is no such UI field. For the other ones it is added automatically in the function <strong>register_user</strong>. The same thing happens when I try to login any user by using standard login/password form.</p>
  
  <p>Workflow update:<br/>
  1. PHP:</p>
  
  <pre class="lang-php prettyprint-override"><code>       $user = load_user(request['usertype'], request['specific_id'])
         if not load then register_user(request['username'], request['usertype'], request['specific_id'])
         //wp_clear_auth_cookie();
         $usr = wp_set_current_user ( $user-&gt;ID, $user-&gt;user_login);
         wp_set_auth_cookie  ( $user-&gt;ID );
         do_action( 'wp_login', $user-&gt;user_login );
         return $user; //&lt;-- this is returned to javascript Ajax request
      }
  </code></pre>
  
  <ol start="2">
  <li>Javascript:</li>
  </ol>
  
  <pre class="lang-js prettyprint-override"><code>    function onClick()
      {
         let xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function()
            {
              if (this.readyState == 4 &amp;&amp; this.status == 200)
              {
                  window.location = redirectaddress; //redirect
                  //or window.location=window.location;//just refresh
              }
            }
         xhttp.open("POST", "/wp-json/register_or_login", true);
         xhttp.setRequestHeader("Content-type", "application/json");
         xhttp.send(JSON.stringify(request));
      }
  </code></pre>