So, I’m working on a plugin in which I’m making a custom page containing a login form as well as one for user registration; I have the forms built and operational, below is my code.
<!-- Begin login form -->
<form name="loginform" id="login_form" class="login_form" action="<?php echo esc_url( wp_login_url() ); ?>" method="post">
<p>
<input type="text" name="log" id="user_login" class="input" placeholder="Username" />
</p>
<p>
<input type="password" name="pwd" id="user_pass" class="input" placeholder="Password" />
</p>
<button name="wp-submit" id="wp-submit" class="btn"><?php _e("Sign in", "shorti"); ?></button>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER["REQUEST_URI"]; ?>" />
</form>
<!-- end login form -->
<!-- Begin registration form -->
<form method="post" id="register_form" class="wp-user-form" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>">
<p><!-- Username -->
<input type="text" name="user_login" id="user_login" class="input" placeholder="username" />
</p>
<p><!-- Email to send p/w to -->
<input type="email" name="user_email" id="user_email" class="input" placeholder="email address" />
</p>
<p class="small-text">You will receive an email with a generated password<br />(which you can change in your "user settings")</p>
<?php do_action('register_form'); ?>
<button name="wp-submit" id="wp-submit" class="btn"><?php _e("Register!", "shorti"); ?></button>
<?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
<input type="hidden" name="user-cookie" value="1" />
</form><!-- end registration-form -->
This works fine beside the fact that whenever there’s an error message it redirects the user to wp-login.php. I’m needing the page with the form to handle all the errors with AJAX; so as to give a better user experience. What is the best way to go about doing this?
Look at the opening form element. The value for action determines where the form data is being posted to. By changing the value you can simply tell the form to redirect to itself rather than another page.
The codex offers an alternative approach on generating the mark-up for the necessary form:
http://codex.wordpress.org/Customizing_the_Login_Form#Make_a_Custom_Login_Page
Here’s a much more in-depth tutorial:
http://digwp.com/2010/12/login-register-password-code/
You could always just use or fork an already existing popular Ajax based log-in plugin:
http://wordpress.org/plugins/login-with-ajax/
Here is what I do. Use login form to create login https://codex.wordpress.org/Function_Reference/wp_login_form
Below are the login logout redirects and login error handling. This will help you with login issue.