0

Until now I was able to create the "Custom Register Form" on my Wordpress website, and send an activation mail to the registered user with the link for the activation.

What I want to do is soon after the registration process, the user is redirected to a "Thank you" page. But when i add to the code the wp_redirect(); it gives the error of "Cannot modify header information - headers already sent by..."

Below is the code for the "Custom Register Form"

  <?php
/*
Template Name: Register Page
*/
?>
<?php wp_head(); the_post(); ?>

      <div class="half-d" id="account-log">

            <form method="post">

        <label class="reg-form-lab p-label">Last Name</label>
        <input type="text" value="" name="last_name" id="last_name" class="reg-form-inp"/>
        <label class="reg-form-lab">First Name</label>
        <input type="text" value="" name="first_name" id="first_name" class="reg-form-inp"/>
        <label class="reg-form-lab p-label">Email</label>
        <input type="text" value="" name="email" id="email" class="reg-form-inp"/>
        <label class="reg-form-lab p-label">Username</label>
        <input type="text" value="" name="username" id="username" class="reg-form-inp"/>
        <label class="reg-form-lab p-label">Password</label>
        <input type="password" value="" name="pwd1" id="pwd1" class="reg-form-inp"/>
        <label class="reg-form-lab p-label">Password again</label>
        <input type="password" value="" name="pwd2" id="pwd2" class="reg-form-inp"/>
        <div class="alignleft"><p><?php if($sucess != "") { echo $sucess; } ?> <?php if($err != "") { echo $err; } ?></p></div>
        <button type="submit" name="btnregister" class="button" id="wp-submit" >Submit</button>
        <input type="hidden" name="task" value="register" />
    </form>

      </div>


      <div class="half-d" id="social-log">


        <p class="new-acc" id="iscr"><a href="https://mywebpage/login">Sign in</a></p>


      </div>


<?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( $email == "" || $pwd1 == "" || $pwd2 == "" || $username == "" || $first_name == "" || $last_name == "") {
            $err = 'Please don\'t leave the required fields.';
        } else if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $err = 'Invalid email address.';
        } else if(email_exists($email) ) {
            $err = 'Email already exist.';
        } else if($pwd1 <> $pwd2 ){
            $err = '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', $pwd1), 'user_login' => apply_filters('pre_user_user_login', $username), 'user_email' => apply_filters('pre_user_user_email', $email), 'role' => 'subscriber' ) );
            if( is_wp_error($user_id) ) {
                $err = 'Error on user creation.';
            } else {
                do_action('user_register', $user_id);
                $success = 'You\'re successfully register';
            }

        }
        //wp_redirect('xxxxxxxxxxxxxxxxxx');
    }
?>

And the code that I insert into functions.php file in order to send the activation email is below:

add_action( 'user_register', 'actuser_user_register' );
function actuser_user_register( $user_id ) {

$user = get_user_by('id',$user_id);

$user_email = stripslashes($user->user_email);

 $code = sha1( $user_id . time() );

update_user_meta( $user_id, 'activation_key', $code );
update_user_meta( $user_id, 'activation_flag', 0 );

$activation_link = add_query_arg( array( 'key' => $code, 'user' => $user_id ), get_permalink($id));

$firstName = get_user_meta($user->ID, "first_name", true);
$lastName = get_user_meta($user->ID, "last_name", true);

$to = $user_email;
$subject = 'Activation Required';
$message = 'xxxxxxxxxxxxxxx';


wp_mail( $to, $subject, $message);
//wp_redirect('xxxxxxxxxx');
}
  • And where do you paste that code? – Krzysiek Dróżdż May 25 '18 at 6:45
  • I have included all the code for the register page – denis May 25 '18 at 7:02
0

OK, so it's clear that the redirection won't work in this case. You can't output any HTML before sending headers, and redirection sends a header.

The easiest (but bad) solution is to move you PHP code to the top of that page template:

<?php
    /* Template Name: Register Page */

    $err = '';
    $success = '';

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

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

        ...
        wp_redirect('xxxxxxxxxxxxxxxxxx');
        die();
    }

    wp_head(); the_post();
?>
  ...
  <div class="half-d" id="account-log">

        <form method="post">
        ...
        </form>

  </div>
  ...

You also have to die the script after doing redirection.

But... This isn't very good way to do this. You should not process any forms directly in templates - templates are for displaying site and not for processing any data and any logic.

Much nicer way would be to send that form to wp_admin_url('admin-post.php)' and processing it using [admin_post_(action)`]1.

  • 2
    Woww, thank you so much. I tried the easiest (but bad) solution. It worked. Now I am going to implement the admin-post.php solution. – denis May 25 '18 at 7:31
0

Use this hook for redirect to another page after registers user.

add_action( 'user_register', 'thankyou_page_redirect', 10, 1 );

function thankyou_page_redirect( $user_id ) {

    $thankyoupage  = site_url('thank-you');

    wp_redirect($thankyoupage);
    exit;

}
  • It won't work, since headers are already sent before user is registered. – Krzysiek Dróżdż May 25 '18 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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