title: Automatic Login after Registration - Wordpress
body: <p>I am working on a site where I have a frontend registration form and it works as it should but now I am having difficulty with automatic login and redirect upon successful registration. I've tried a few plugins and codes in my themes functions file but none seem to work. So how can I automatically log a person in after a successful registration? Any help would be appreciated thanks.</p>
  
  <pre><code>&lt;?php
  if(get_option('users_can_register')) {
  //Check whether user registration is enabled by the administrator
  ?&gt;
  
  &lt;?php
  
  if($_POST){
      //We shall SQL escape all inputs
      $username = $wpdb-&gt;escape($_REQUEST['username']);
      if(empty($username)) {
          echo "&lt;span style='color:#FF0000'&gt;&lt;strong&gt;Error..&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;You have to fill in the username.";
          exit();
      }
      $email = $wpdb-&gt;escape($_REQUEST['email']);
      if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
          echo "&lt;span style='color:#FF0000'&gt;&lt;strong&gt;Error..&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;please use a calid e-mailadress.";
          exit();
      }
  
      $pass1 = $wpdb-&gt;escape($_REQUEST['pass1']);
  $pass2 = $wpdb-&gt;escape($_REQUEST['pass2']);
  if ($pass1 != $pass2){
      echo "&lt;span style='color:#FF0000'&gt;&lt;strong&gt;Error..&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;please use a passwords don't match.";
          exit();
  
  }
  $random_password = $pass1;
      $status = wp_create_user( $username, $random_password, $email );
      if ( is_wp_error($status) )
          echo "&lt;span style='color:#FF0000'&gt;&lt;strong&gt;Feil..&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Username allready exist. please try another one.";
      else {
          $from = get_option('admin_email');
          $headers = 'From: '.$from . "\r\n";
          $subject = "Registration ok!";
          $msg = "Welcome, you are now registered. Here is your username and password.\Info:\Username: $username\Password: $random_password";
          wp_mail( $email, $subject, $msg, $headers );
          echo "&lt;strong&gt;You are now registered. An e-mail is now sent to you with your username and password..";
      }
  
      exit();
  
  }
   else
  {
  //Embed the register form and javascript here
  
  ?&gt;
  
  
  &lt;div id="result"&gt;&lt;/div&gt;
  &lt;div style="padding-top:5px;"&gt;&lt;h2 style="font-size:16px;color:#06f;"&gt;Register&lt;/h2&gt;&lt;/div&gt;
  
  &lt;form id="wp_signup_form" action="" method="post"&gt;
  &lt;p&gt;&lt;label&gt;Username&lt;br /&gt;
  &lt;input type="text" name="username" style="width:250px; margin-bottom:3px;"&gt;&lt;/label&gt;&lt;/p&gt;
  &lt;p&gt;&lt;label&gt;E-Mail&lt;br /&gt;
  &lt;input type="text" name="email" style="width:250px; margin-bottom:3px;"&gt;&lt;/label&gt;&lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Password&lt;br /&gt;
  &lt;input type="password" name="pass1" style="width:250px; margin-bottom:3px;"&gt;&lt;/label&gt;&lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Repeat Password&lt;br /&gt;
  &lt;input type="password" name="pass2" style="width:250px; margin-bottom:3px;"&gt;&lt;/label&gt;&lt;/p&gt;
  &lt;br /&gt;
  &lt;input type=checkbox name="termsnp"&gt;By registering I agree to the terms and policy&lt;br /&gt;&lt;br /&gt;
  &lt;input type="submit" id="submitbtn" name="submit" value="Register" class="knapp" style="padding:8px;"&gt;
  &lt;/form&gt;
  &lt;script type="text/javascript"&gt;                         
  $("#submitbtn").click(function() {
  $('#result').html('&lt;img src="&lt;?php bloginfo('template_url'); ?&gt;/images/loader.gif" class="loader" /&gt;').fadeIn();
  var input_data = $('#wp_signup_form').serialize();
  $.ajax({
  type: "POST",
  url:  "&lt;?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?&gt;",
  data: input_data,
  success: function(msg){
  $('.loader').remove();
  $('
  &lt;div&gt;').html(msg).appendTo('div#result').hide().fadeIn('slow');
  }
  });
  return false;
  
  });
  &lt;/script&gt;
  &lt;?php
  // embedded
  
  }
  ?&gt;
  
  &lt;?php
      }
  ?&gt;
  </code></pre>