Guest User

Untitled

a guest
Jan 9th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. register_rest_route( $namespace, '/' . $base . '/reset', array(
  2. array(
  3. 'methods' => 'POST',
  4. 'callback' => array( $this, 'myplugin_reset_user_pass' ),
  5. ),
  6. ));
  7.  
  8. public function myplugin_reset_user_pass( $request ) {
  9.  
  10. $params = $request->get_params();
  11.  
  12. $userdata = array(
  13. 'user_email' => $params['user_email']
  14. );
  15.  
  16. $user = get_user_by( 'email', $userdata['user_email'] );
  17.  
  18. // check if username / email exist
  19. $username = username_exists( $user->user_login );
  20. if ( $username and email_exists($user->user_email) == true ) {
  21. if ( function_exists( 'wp_set_password') ) {
  22. // generate new password
  23.  
  24. $password = wp_generate_password( 8, true );
  25. $resetPass = wp_set_password( $password, $user->id );
  26.  
  27. if($resetPass){
  28. return new WP_REST_Response(true, 200);
  29. }
  30. }
  31. }
  32. return new WP_Error( 'cant-reset', __( 'message', 'text-domain'), array( 'status' => 500 ) );
  33. }
Add Comment
Please, Sign In to add comment