0

I have another wordpress dilemma... I've built a frontend registration form, but for some reason whenever a user tries to register it doesn't work, and they get redirected to wp-login.php.

The following code does 5 things:

  1. gets user fields from a form (at the bottom),
  2. validates the data,
  3. inserts a new user into the database on success,
  4. logs the new guy in,
  5. sets the current_user to the new user

Here is the form / form head:

<?php
$err = '';
$success = '';

global $wpdb, $PasswordHash, $current_user, $user_ID;


if(isset($_POST['task']) && $_POST['task'] == 'register' ) { 

$pwd1 = $wpdb->escape(trim($_POST['pwd1']));
$pwd2 = $wpdb->escape(trim($_POST['pwd2']));
$first_name = $wpdb->escape(trim($_POST['first_name']));
$last_name = $wpdb->escape(trim($_POST['last_name']));
$email = $wpdb->escape(trim($_POST['email']));
$username = $wpdb->escape(trim($_POST['username']));

// if( false ) {
if( $email == "" || $pwd1 == "" || $pwd2 == "" || $username == "" ) {
    $err .= "Please don't leave out any required fields.<br>";
} 
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $err .= "Invalid email address.<br>";
} 
if(email_exists($email) ) {
    $err .= "Email already exist<br>.";
} 
if($pwd1 <> $pwd2 ){
    $err .= "Passwords do not match.<br>";
} 
// if no error
if ($err == "") {
    $user_id = wp_insert_user( 
        array (
            'first_name' => apply_filters('pre_user_first_name', $first_name),
            'last_name' => apply_filters('pre_user_last_name', $last_name),
            'user_pass' => apply_filters('pre_user_pass', $pwd1),
            'user_login' => apply_filters('pre_user_login', $username),
            'user_email' => apply_filters('pre_user_email', $email),
            'role' => 'subscriber' 
        )
    );

    if( is_wp_error($user_id) ) {
        $err = 'Error on user creation.';
    } else {

        if(isset($_REQUEST['redirurl'])) {
            $url = $_REQUEST['redirurl']; // holds url for last page visited.
        } else { 
           $url = '/'; // default page for 
        }
        header("Location:$url");  


        do_action('user_register', $user_id);

        $success = 'You\'ve successfully registered';

        $creds['user_login'] = $username;
        $creds['user_password'] = $pwd1;
        $creds['remember'] = true;
        $autologin_user = wp_signon($creds, false); // automatically log the new user in

        $user = wp_set_current_user($user_id); // set the current user to the user who just registered

    }

}

}
?>
<div class="alignleft">
<p>
    <?php if($sucess != "") { echo $sucess; } ?> <?php if($err != "") { echo $err; } ?>
</p>
</div>
<div class="<?php echo $err ? '' : 'hidden animated fadeInDown duration2'; ?>" id="registrationForm">
<!-- Start form -->
<form method="post" action="#">
    <?php var_dump($_POST); ?>
    <h1>Register</h1>
    <h3>Account Detail</h3>
    <!-- <input type="hidden" name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" /> -->
    <p>
        <label>Username*</label>
        <input type="text" value="" name="username" id="username" />
    </p>
    <p>
        <label>Password*</label>
        <input type="password" value="" name="pwd1" id="pwd1" />
    </p>

    <p>
        <label>Confirm Password*</label>
        <input type="password" value="" name="pwd2" id="pwd2" />
    </p>

    <h3>Contact</h3>
    <p>
        <label>Email*</label>
        <input type="text" value="" name="email" id="email" />
    </p>
    <p>
        <label>First Name</label>
        <input type="text" value="" name="first_name" id="first_name" />
    </p>
    <p>
        <label>Last Name</label>
        <input type="text" value="" name="last_name" id="last_name" />
    </p>            



    <p>
        <button type="submit" name="btnregister" class="button" >Submit</button>
        <input type="hidden" name="task" value="register" />
    </p>
</form>
</div><!-- end registration form -->

And here is the snippet from the functions.php file that doesn't let wordpress redirect to wp-login.php on login errors:

/**
* Disable redirection upon login error
*/
function no_wordpress_errors(){
  return header('Location: /login/?login=failed');
  exit;
}
add_filter( 'login_errors', 'no_wordpress_errors' );

hint: there may be MANY things wrong with this code. The problem persists with or without the header function.

Thanks for your help!

Jill

6
  • well, I'm trying to STOP the redirect that persists.... and yes, login_errors is doing it's job... thanks for the quick response! Oct 30, 2018 at 14:54
  • The problem is probably here: <form method="post" action="/login"> - do you actually have a /login page? If yes, use get_permalink( 123 ) where 123 is the page ID. Otherwise, set it to '' (i.e. submits to the current page).
    – Sally CJ
    Oct 30, 2018 at 16:40
  • This also doesn't work: header("Location:$url"); unless you do exit(); right after that, but only if there were no output (HTML tags, empty spaces, etc.) sent to the browser. And you should also redirect after the wp_signon() call in order for the login cookies to be properly set in the browser. So I suggest you to separate the form and the code that handles the form submission (and login/redirect stuff) - e.g. hook to init and process the registration/login from there.
    – Sally CJ
    Oct 30, 2018 at 17:12
  • Whoops, I can see why that's not clear. Updating my question accordingly - I originally set the action to #, which just brought the user to wp-login.php#. That may shed some light on what's going on here.. Two more notes:/login, is the current page, and including or not including the header function makes no difference in this case. Oct 30, 2018 at 18:33
  • I had used <form method="post" action="#">, which is virtually the same (tell me if that's not right), but still - none of the data saved, and the user was still being redirected to wp-login - header() and signon() commented out and all... Oct 31, 2018 at 13:45

0

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.