Skip to content

Instantly share code, notes, and snippets.

@bkaminski
Forked from ivandoric/gist:e4e46294c4d35eac0ec8
Last active March 22, 2018 16:49
Show Gist options
  • Save bkaminski/c97e94ba998ea86105d25528c2686c86 to your computer and use it in GitHub Desktop.
Save bkaminski/c97e94ba998ea86105d25528c2686c86 to your computer and use it in GitHub Desktop.
wordpress: create custom reset password page
// With some Bootstrap 4 love. Of course, create a custom template for this.
<?php
/* do template stuff */
get_header(); ?>
<div class="card-body">
<?php
global $wpdb;
$error = '';
$success = '';
if( isset( $_POST['action'] ) && 'reset' == $_POST['action'] ) {
$email = trim($_POST['user_login']);
if( empty( $email ) ) {
$error = 'The Email field can not be empty. Enter the email used to register with K911 to reset your password.';
} else if( ! is_email( $email )) {
$error = 'Please use your email address for account recovery. We do not permit username entry for security purposes. If you are still experincing problems, please contact us. ';
} else if( ! email_exists( $email ) ) {
$error = 'There is no user registered with that email address. Please create an account or contact us for assistance.';
} else {
$random_password = wp_generate_password( 12, false );
$user = get_user_by( 'email', $email );
$update_user = wp_update_user( array (
'ID' => $user->ID,
'user_pass' => $random_password
));
if( $update_user ) {
$to = $email;
$subject = 'K911 Password Reset Request';
$sender = get_option('name');
$message = 'Hello, Your new temporary password for K911online is:<br /><br />'. $random_password .'<br /><br/ >Upon successful login, please visit your user profile and update your password to something more memorable and secure. Contact us for further assistance. <br /><br />http://k911online.com/login-or-register/';
$headers[] = 'MIME-Version: 1.0' . "\r\n";
$headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers[] = "X-Mailer: PHP \r\n";
$headers[] = 'From: '.$sender.' < '.$email.'>' . "\r\n";
$mail = wp_mail( $to, $subject, $message, $headers );
if( $mail )
$success = 'Please check your email for a message containing password reset information.';
} else {
$error = 'It seems that something went wrong while trying to process your request. Please try again later.';
}
}
if( ! empty( $error ) )
echo '<div class="alert alert-danger"><strong>ERROR:</strong> '. $error .'</div>';
if( ! empty( $success ) )
echo '<div class="alert alert-success">'. $success .'</div>';
}
?>
<form method="post">
<fieldset>
<p>Please enter your email address used to login. You will receive a link to create a new password via email.</p>
<div class="form-group">
<label for="user_login">E-mail:</label>
<?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
<input type="text" name="user_login" class="form-control-lg" id="user_login" value="<?php echo $user_login; ?>" />
</div>
<div class="form-group">
<input type="hidden" name="action" value="reset" />
<input type="submit" value="Get New Password" class="btn btn-success btn-rounded-dark" id="submit" />
</div>
</fieldset>
</form>
</div>
<?php get_footer(); ?>
@bkaminski
Copy link
Author

bkaminski commented Mar 10, 2018

Realizing that the original gist did not contain any actual "username" validation (After testing in WP 4.9.x) in the code, I changed the verbiage to get people to only use their email address for password reset. May eventually add $current_user->user_login to this to actually check for usernames, but I don't mind simply using email validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment