Search code examples
phpwordpressformshttp-redirectuser-registration

Wordpress redirect to profile page after user registration


I'm trying to redirect users after successful registration to their profile page but no matter where I put the redirect it doesn't seem to work.

The user is being registered correctly but the redirect isn't happening.

All the code seems to be functioning correctly by outputting the correct error messages when there is something that doesn't validate and also registering the user if the information is valid. The only issue is the redirect.

This is the error I am now receiving: Notice: wpdb::escape is deprecated since version 3.6.0! Use wpdb::prepare() or esc_sql() instead. My PHP is:

<?php
        $success = '';
        $error = '';
        global $wpdb, $PasswordHash, $current_user, $user_ID;

        if(isset($_POST['task']) && $_POST['task'] == 'register' ) {
            $password1 = $wpdb->escape(trim($_POST['password1']));
            $password2 = $wpdb->escape(trim($_POST['password2']));
            $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['email']));

            if( $email == "" || $password1 == "" || $password2 == "" || $first_name == "" || $last_name == "") {
                $error = 'Please don\'t leave the required fields.';
            } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $error = 'Invalid email address.';
            } else if(email_exists($email) ) {
                $error = 'Email already exists.';
            } else if($password1 <> $password2 ){
                $error = 'Password do not match.';      
            } else {
                $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_user_pass', $password1), 'user_login' => apply_filters('pre_user_user_login', $username), 'user_email' => apply_filters('pre_user_user_email', $email), 'role' => 'young-person') );
                if( is_wp_error($user_id) ) {
                    $error = 'Error on user creation.';
                } else {
                    do_action('user_register', $user_id); 
                    wp_redirect ( home_url("/profile") );
                }
            }
        } ?>

And my HTML is:

<form method="post">
            <div class="row">
                <div class="six">
                    <p><label>First Name</label>
                    <input type="text" value="" name="first_name" id="first_name" /></p>
                </div>
                <div class="six">
                    <p><label>Last Name</label>
                    <input type="text" value="" name="last_name" id="last_name" /></p>
                </div>
                <div class="six">
                    <p><label>Email</label>
                    <input type="text" value="" name="email" id="email" required /></p>
                </div>
                <div class="six">
                    <p><label>Password</label>
                    <input type="password" value="" name="password1" id="password1" /></p>
                </div>
                <div class="six">
                    <p><label>Repeat password</label>
                    <input type="password" value="" name="password2" id="password2" /></p>
                </div>
               <div class="full">
                   <div class="alignleft"><?php if($error!= "") { echo $error; } ?></div>


                    <button type="submit" name="btnregister" class="button" >Submit</button>
                    <input type="hidden" name="task" value="register" />
                </div>
            </div>  
        </form>

Any help would be massively appreciated.


Solution

  • Have you disabled error reporting? I suspect you're outputting something before the redirect causing a PHP Warning: Cannot modify header information that's being suppressed (i.e. not shown), you might want to check your logs.

    (Not so )long story short: check whether you're printing, echoing, etc., anything before the redirect. That will cause the redirect to fail.

    Additional notes:

    1. You may want to throw in an die('test') between the calls to do_action() and wp_direct() just to check whether this point is actually reached -- I suspect it is.
    2. You also want to add an exit after the redirect. Check this SO question why. And also the WordPress documentation on wp_redirect():

      Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;.