-
Notifications
You must be signed in to change notification settings - Fork 3
/
page-forgot-password.php
95 lines (76 loc) · 3.07 KB
/
page-forgot-password.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Template Name: Forgot Password
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
*
* @package CalCoin
*/
get_header();
?>
<div class="row align-center">
<div class="columns small-10">
<img src="<?php echo get_stylesheet_directory_uri(); ?>/assets/img/login-logo.png">
</div>
</div>
<div class="row">
<div class="columns">
<div class="card card-padded">
<?php
global $wpdb;
$error = '';
$success = '';
// check if we're in reset form
if( isset( $_POST['action'] ) && 'reset' == $_POST['action'] )
{
$email = trim($_POST['user_login']);
if( empty( $email ) ) {
$error = 'Enter a username or e-mail address.';
} else if( ! is_email( $email )) {
$error = 'Invalid username or e-mail address.';
} else if( ! email_exists( $email ) ) {
$error = 'There is no user registered with that email address.';
} 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 return true then lets send user an email containing the new password
if( $update_user ) {
$to = $email;
$subject = 'Your new password';
$sender = get_option('name');
$message = 'Your new password is: '.$random_password;
$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 = 'Check your email address for you new password.';
} else {
$error = 'Oops something went wrong updaing your account.';
}
}
if( ! empty( $error ) )
echo '<div class="message"><p class="error"><strong>ERROR:</strong> '. $error .'</p></div>';
if( ! empty( $success ) )
echo '<div class="error_login"><p class="success">'. $success .'</p></div>';
}
?>
<form class="reset-form" method="post">
<p>Please enter your username or email address. You will receive a link to create a new password via email.</p>
<label for="user_login">Username or E-mail:</label>
<?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
<input type="text" name="user_login" id="user_login" value="<?php echo $user_login; ?>" />
<input type="hidden" name="action" value="reset" />
<input type="submit" value="Get New Password" class="button button-primary" id="submit" />
</form>
</div>
</div>
</div>
<?php
get_footer();