I set up a front end form for logged in users to update their email address. So far it is working however I want the user, after confirming the email change via link sent to the new address, to be redirected to a custom front-end page.(after the change done in /wp-admin/edit-profile.php).
Thus the user never seeing the backend user profile page.
Here is the code for the form
<form action="<?php the_permalink(); ?>" method="post">
<div class="form-group">
<label for="e-mail">E-mail</label>
<input type="email" class="form-control" value="<?php echo esc_html($current_user->user_email); ?>" name="email" id="email">
<small class="form-text text-muted">If you change this we will send you an email at your new address to confirm it. <strong>The new address will not become active until confirmed.</strong></small>
</div>
<button class="btn btn-primary" type="submit">Change E-mail</button>
</form>
and the form processing
if (isset( $_POST[\'email\'])) {
// check if user is really updating the value
if ($user_email != $_POST[\'email\']) {
// check if email is free to use
if (email_exists( $_POST[\'email\'] )){
// email already taken
echo \'That e-mail address is not available.\';
exit();
} else {
$_POST[\'user_id\'] = $current_user->ID;
send_confirmation_on_profile_email();
echo \'User update email ink sent to new email for verification.\';
}
}else{
//same email
echo \'The email you entered is the same as your current email.\';
}
}
This is the default code in edit-profile.php that handles the email change. Could I add a redirect here somewhere after email change. Is that a good idea?
// Execute confirmed email change. See send_confirmation_on_profile_email().
if ( IS_PROFILE_PAGE && isset( $_GET[\'newuseremail\'] ) && $current_user->ID ) {
$new_email = get_user_meta( $current_user->ID, \'_new_email\', true );
if ( $new_email && hash_equals( $new_email[\'hash\'], $_GET[\'newuseremail\'] ) ) {
$user = new stdClass;
$user->ID = $current_user->ID;
$user->user_email = esc_html( trim( $new_email[\'newemail\'] ) );
if ( is_multisite() && $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $current_user->user_login ) ) ) {
$wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, $current_user->user_login ) );
}
wp_update_user( $user );
delete_user_meta( $current_user->ID, \'_new_email\' );
wp_redirect( add_query_arg( array( \'updated\' => \'true\' ), self_admin_url( \'profile.php\' ) ) );
die();
} else {
wp_redirect( add_query_arg( array( \'error\' => \'new-email\' ), self_admin_url( \'profile.php\' ) ) );
}
} elseif ( IS_PROFILE_PAGE && ! empty( $_GET[\'dismiss\'] ) && $current_user->ID . \'_new_email\' === $_GET[\'dismiss\'] ) {
check_admin_referer( \'dismiss-\' . $current_user->ID . \'_new_email\' );
delete_user_meta( $current_user->ID, \'_new_email\' );
wp_redirect( add_query_arg( array( \'updated\' => \'true\' ), self_admin_url( \'profile.php\' ) ) );
die();
}