Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include('components/cls-forgot-password.php'); // including custom forgot password shortcode class
- $CLS_FORGOT_PASSWORD = new CLS_FORGOT_PASSWORD; // invoking the access for the cls_forgot_password_form shortcode
- function redirect_forgot_page(){
- // Store for checking if this page equals wp-login.php
- $page_viewed = basename( $_SERVER['REQUEST_URI'] );
- if( $page_viewed == "wp-login.php?action=lostpassword" ) {
- wp_redirect( '/forgot');
- exit();
- }
- }
- add_filter("init", "redirect_forgot_page");
- <?php
- if ( ! defined( 'ABSPATH' ) ) { die; }
- if ( ! class_exists( 'CLS_FORGOT_PASSWORD' ) ) {
- class CLS_FORGOT_PASSWORD {
- function __construct() {
- add_shortcode('cls_forgot_password_form', array( $this, 'shortcode_init') );
- }
- public function shortcode_init($atts, $content = null) {
- ob_start();
- $this->process();
- $this->form();
- return ob_get_clean();
- }
- private function form() {
- ?>
- <p>Please enter your username or email address. You will receive a link to create a new password via email.</p>
- <hr />
- <div class="page-restrict-output">
- <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
- <input type="hidden" name="action" value="reset" />
- <div class="login-form">
- <div class="form-group">
- <?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
- <label class="login-field-icon fui-user" for="login-name">Username: </label>
- <input name="user_login" type="text" class="login-field" value="" placeholder="Username or email address" id="login-name" />
- </div>
- <p align="center">
- <input class="btn btn-default" type="submit" name="dlf_submit" value="Get New Password" />
- </p>
- </div>
- </form>
- </div>
- <?php
- }
- private function process() {
- 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 {
- // lets generate our new password
- $random_password = wp_generate_password( 12, false );
- // Get user data by field and data, other field are ID, slug, slug and login
- $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="error_login"><strong>ERROR:</strong> '. $error .'</div>';
- if( ! empty( $success ) )
- echo '<div class="updated"> '. $success .'</div>';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement