Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. include('components/cls-forgot-password.php'); // including custom forgot password shortcode class
  2. $CLS_FORGOT_PASSWORD = new CLS_FORGOT_PASSWORD; // invoking the access for the cls_forgot_password_form shortcode
  3.  
  4. function redirect_forgot_page(){
  5. // Store for checking if this page equals wp-login.php
  6. $page_viewed = basename( $_SERVER['REQUEST_URI'] );
  7.  
  8. if( $page_viewed == "wp-login.php?action=lostpassword" ) {
  9. wp_redirect( '/forgot');
  10. exit();
  11. }
  12. }
  13.  
  14. add_filter("init", "redirect_forgot_page");
  15.  
  16.  
  17.  
  18.  
  19.  
  20. <?php
  21. if ( ! defined( 'ABSPATH' ) ) { die; }
  22.  
  23. if ( ! class_exists( 'CLS_FORGOT_PASSWORD' ) ) {
  24.  
  25. class CLS_FORGOT_PASSWORD {
  26. function __construct() {
  27. add_shortcode('cls_forgot_password_form', array( $this, 'shortcode_init') );
  28. }
  29. public function shortcode_init($atts, $content = null) {
  30. ob_start();
  31. $this->process();
  32. $this->form();
  33. return ob_get_clean();
  34.  
  35. }
  36. private function form() {
  37. ?>
  38. <p>Please enter your username or email address. You will receive a link to create a new password via email.</p>
  39. <hr />
  40. <div class="page-restrict-output">
  41.  
  42. <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
  43. <input type="hidden" name="action" value="reset" />
  44.  
  45. <div class="login-form">
  46. <div class="form-group">
  47. <?php $user_login = isset( $_POST['user_login'] ) ? $_POST['user_login'] : ''; ?>
  48. <label class="login-field-icon fui-user" for="login-name">Username: </label>
  49. <input name="user_login" type="text" class="login-field" value="" placeholder="Username or email address" id="login-name" />
  50. </div>
  51.  
  52. <p align="center">
  53. <input class="btn btn-default" type="submit" name="dlf_submit" value="Get New Password" />
  54. </p>
  55. </div>
  56. </form>
  57.  
  58. </div>
  59.  
  60. <?php
  61. }
  62. private function process() {
  63. global $wpdb;
  64.  
  65. $error = '';
  66. $success = '';
  67.  
  68. // check if we're in reset form
  69. if( isset( $_POST['action'] ) && 'reset' == $_POST['action'] )
  70. {
  71. $email = trim($_POST['user_login']);
  72.  
  73. if( empty( $email ) ) {
  74. $error = 'Enter a username or e-mail address..';
  75. } else if( ! is_email( $email )) {
  76. $error = 'Invalid username or e-mail address.';
  77. } else if( ! email_exists($email) ) {
  78. $error = 'There is no user registered with that email address.';
  79. } else {
  80.  
  81. // lets generate our new password
  82. $random_password = wp_generate_password( 12, false );
  83.  
  84. // Get user data by field and data, other field are ID, slug, slug and login
  85. $user = get_user_by( 'email', $email );
  86.  
  87. $update_user = wp_update_user( array (
  88. 'ID' => $user->ID,
  89. 'user_pass' => $random_password
  90. )
  91. );
  92.  
  93. // if update user return true then lets send user an email containing the new password
  94. if( $update_user ) {
  95. $to = $email;
  96. $subject = 'Your new password';
  97. $sender = get_option('name');
  98.  
  99. $message = 'Your new password is: '.$random_password;
  100.  
  101. $headers[] = 'MIME-Version: 1.0' . "\r\n";
  102. $headers[] = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  103. $headers[] = "X-Mailer: PHP \r\n";
  104. $headers[] = 'From: '.$sender.' < '.$email.'>' . "\r\n";
  105.  
  106. $mail = wp_mail( $to, $subject, $message, $headers );
  107. if( $mail )
  108. $success = 'Check your email address for you new password.';
  109.  
  110. } else {
  111. $error = 'Oops something went wrong updaing your account.';
  112. }
  113.  
  114. }
  115.  
  116. if( ! empty( $error ) )
  117. echo '<div class="error_login"><strong>ERROR:</strong> '. $error .'</div>';
  118.  
  119. if( ! empty( $success ) )
  120. echo '<div class="updated"> '. $success .'</div>';
  121. }
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement