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');
}