I’ve recently tried to add a user registration form to the front end of my site and have used the following form code (based upon this I discovered in an answer by maugly in question Front-end Register Form) in a template file:
<div id="tab2_login" class="tab_content_login" >
<h3>Add a child</h3>
<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" class="wp-user-form">
<div class="username">
<label for="user_login"><?php _e('Child Name'); ?>: </label>
<input type="text" name="user_login" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" />
</div>
<div class="email">
<input type="text" name="user_email" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" id="user_email" />
</div>
<div class="login_fields">
<?php do_action('register_form'); ?>
<input type="submit" name="user-submit" value="add child" class="user-submit" />
<?php
$register = $_GET['register'];
if($register == true) {
//now add it to the link database
echo '<p>User Added!</p>';
} ?>
<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
<input type="hidden" name="user-cookie" value="1" />
</div>
</form>
</div>
The redirect_to
attribute is set here to the requesting page which is all great when it works by triggering the if block to display an additional success message.
The question I have is when an error is detected in wp-login.php
(such as the username is already in use) how do I get the redirect to come back to this page as opposed to the wp-login generated login box/error display? I can’t see any obvious hook to grab hold of (although I’ve never used actions yet, so could be missing something!)
Help and guidance very much appreciated!
Ryan
Hook into registration_errors (The WP Codex is woefully incomplete on this, but see links below).
Sorry this isn’t more helpful — the issue is a little more complex than it seems at the outset. I’ll look into it more later.
Links:
If I understand you correctly, you need to check if the username is already registered, and if so, redirect back to the register form ? or something.
I am thinking you need to use the auth_redirect() function in WP.
This isn’t going to help you, but it’s something I came up with to redirect users back to the previous page they were on after they log in, instead of going to the Dashboard as WP does when logging in.
Perhaps this can be of some use to you. I am pretty sure you may need to use auth_redirect though.
This redirects errors to the homepage. Put in functions.php
For same page I’m not sure sorry- but with this you can redirect it to a custom error page or anything else.