1

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.

MV-123
  • 367
  • 2
  • 3
  • 14
  • 1
    Where is your PHP being run? Per the answer below, this matters _a lot_, because WP will output content, which would then prevent a redirect from working. Are you positive your query is running successfully? What's the output if you add `var_dump($user_id);` after the query? And lastly - and **most importantly**: You _must_ include an `exit;` or `die();` immediately after the redirect! – random_user_name Jul 03 '18 at 17:11
  • You may want to add relevant errors now that you've enabled error reporting. – Niellles Jul 03 '18 at 17:38

1 Answers1

1

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;.

Niellles
  • 868
  • 10
  • 27
  • This is the error I receive when I enable error reporting: `Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'create_posttype' not found or invalid function name in /home/findcreative/public_html/word/wp-includes/class-wp-hook.php on line 286` even after adding die(); after the redirect – MV-123 Jul 03 '18 at 17:19
  • @MV-123 can you also add a `die('test')` before `do_action()`? If that one is printed remove it and add a `die('test')` directly after `do_action()` (and before the `wp_redirect()`). – Niellles Jul 03 '18 at 17:22
  • @MV-123 - that error is being thrown by something other than the code you shared above, and should get fixed. Search your install for `create_posttype` and fix that issue. – random_user_name Jul 03 '18 at 17:28
  • I have fixed the create_posttype issue. I'm now being thrown this error on submit of the form: `Notice: wpdb::escape is deprecated since version 3.6.0! Use wpdb::prepare() or esc_sql() instead. in /home/findcreative/public_html/word/wp-includes/functions.php on line 3840` – MV-123 Jul 03 '18 at 17:30
  • I fixed that error and on submit I got the "test" response come up. I then moved it to directly after the do_action and got the can not modify headers error – MV-123 Jul 03 '18 at 17:35
  • Alright, so you now have to figure out what (and where) headers are being sent before you run your code... – Niellles Jul 03 '18 at 17:38
  • 1
    The issue is now fixed! I moved my php code to the top of the page before any HTML and everything is running correctly. Thanks for all the help! :) – MV-123 Jul 04 '18 at 17:52
  • Great! Things like that cause this issue... you can't do a redirect after you've sent output.. – Niellles Jul 04 '18 at 18:57