When I want to redirect the user after successful registration, I just use "registration_redirect" filter hook. How about when registration fails, which filter hook should I use? I looked into wp-login.php but I didn't see any filter/action hook that I could use. How do I go about this?
-
Why would registration fail? Normally, a "failure" means that it takes them back to the registration form to fix the error.– OttoCommented Jul 2, 2011 at 15:35
-
@Otto: Mmmm, because I have a custom reg form in a normal page. So I want it to redirect back to that page when registration fails. As of now, it redirects to WP's default reg page which is inconsistent.– jilseegoCommented Jul 2, 2011 at 16:32
2 Answers
You could add an htaccess redirect or try out the theme my login plugin: http://wordpress.org/extend/plugins/theme-my-login/. It has options for custom redirection.
There is an more sophisticated solution instead of htaccess or plugin. For those interested in this topic, there's my code to redirect after (exactly before) unsuccessfull resitration attempt.
Why before? Before an attempt for registration, WordPress checks for an username and email address, whether they are not empty, whether they are not already in DB, and whether email nor username are in correct. If so, it reports errors and you can hook to a function right after this check.
do_action( 'register_post', $sanitized_user_login, $user_email, $errors );
Next step - unhookable - is an attempt to create a new user. If this fails, that there is an another error message and you'll be redirected to standard wp_login.php screen.
But, there is an but. This process, Next step, just calls wp_insert_user() and this function fails only on empty_user_login, existing_user_login and existing_user_email - otherwise it end successfully. And if you've read carefully, all these variables are checked before this function is called (this double check is logical as you can call wp_insert_user directly from your plugin).
So if register process fails after mentioned hook, it fails because there's something wrong with your WordPress installation and so, it is not a fail of your redirection function hooked to register process.
And finally some code for those, ho just want to copy and paste a code for redirection to custom page after login fails:
add_action('register_post', 'binda_register_fail_redirect', 99, 3);
function binda_register_fail_redirect( $sanitized_user_login, $user_email, $errors ){
//this line is copied from register_new_user function of wp-login.php
$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
//this if check is copied from register_new_user function of wp-login.php
if ( $errors->get_error_code() ){
//setup your custom URL for redirection
$redirect_url = get_bloginfo('url') . '/registrace';
//add error codes to custom redirection URL one by one
foreach ( $errors->errors as $e => $m ){
$redirect_url = add_query_arg( $e, '1', $redirect_url );
}
//add finally, redirect to your custom page with all errors in attributes
wp_redirect( $redirect_url );
exit;
}
}
-
Thanks. It's worth noting that to redirect the user on a successful registration, one has to
add_action
forlogin_head
and check whether$_SERVER['REQUEST_URI']
is/wp-login.php?checkemail=registered
, and redirect to the correct one. Supposably there's a filter namedregistration_redirect
, but it doesn't work. Commented Feb 12, 2013 at 8:26 -