1

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

3 Answers 3

1

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:

2
  • hi there - are you saying adding a filter like: add_filter('registration_errors', 'myfunction', 10, 3); will let me define myfunction that sets a new redirect_to attribute? Is it possible to do this in a template file or do I need a plugin to use these hooks? Sorry for the newb questions!
    – user2923
    Feb 4, 2011 at 22:14
  • :-) about as far as I got from your original link. My current thinking is that this seems a ballache! I reckon doing a ajax/jquery check into functions like username_exists() and email_exists() is the way to go...
    – user2923
    Feb 4, 2011 at 22:46
0

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.

function you_must_login() {
    global $post;

    if ( !is_single() ) // if not currently on a single post (can use any other conditional here of your choice)
    return;

    $post_ids = array( 188, 185, 171 ); // array of post IDs that force users to login before reading

    if ( in_array( (int) $post->ID, $post_ids ) && !is_user_logged_in() ) {
        auth_redirect(); // redirect to previous page after logging in
    }
}

Perhaps this can be of some use to you. I am pretty sure you may need to use auth_redirect though.

2
  • Thanks Jared. Certainly a basic and very handy tip, as many people will want to know this. I have reverted to using plugins for this sort of thing now simply because I am lazy and other people code much better than I!
    – user2923
    Feb 23, 2011 at 9:15
  • haha, yeah, i know the feeling. laziness can be a hard thing to overcome. Hence why i made a custom post type that I use to just post code snippets instead of writing blog posts which explain them more in-depth haha.
    – jaredwilli
    Feb 24, 2011 at 11:36
0

This redirects errors to the homepage. Put in functions.php

// block wordpress default login if error for extra security
function prevent_login_access() {
if (strpos(strtolower($_SERVER['REQUEST_URI']), '/wp-login') !== false) {
wp_redirect( home_url() );
}
}

add_action('init', 'prevent_login_access', 0);

For same page I'm not sure sorry- but with this you can redirect it to a custom error page or anything else.

1
  • But that wasn’t the question …
    – fuxia
    Nov 5, 2012 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.