Guest User

Untitled

a guest
Sep 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. $firstname = sanitize_text_field( $_POST['firstname'] );
  2. $lastname = sanitize_text_field( $_POST['lastname'] );
  3. $username = sanitize_text_field( $_POST['reg-username'] );
  4. $user_pass = sanitize_text_field( $_POST['reg-password'] );
  5. $pass_confirm = sanitize_text_field( $_POST['confirm_password'] );
  6. $gender = sanitize_text_field( $_POST['gender'] );
  7. $email = sanitize_text_field( $_POST['email'] );
  8. //Add usernames we don't want used
  9. $invalid_usernames = array( 'admin' );
  10. $errors = array();
  11. //Do username validation
  12. $username = sanitize_user( $username );
  13. if ( empty( $username ) || validate_username( $username ) === false || in_array( $username, $invalid_usernames ) === false ) {
  14. $errors[]= 'Username is invalid.';
  15. }
  16. if ( username_exists( $username ) === false) {
  17. $errors[]= 'Username already exists.';
  18. }
  19. //Do e-mail address validation
  20. if ( !is_email( $email ) ) {
  21. $errors[] = 'E-mail address is invalid.';
  22. }
  23. if (email_exists($email) === false) {
  24. $errors[] = 'E-mail address is already in use.';
  25. }
  26. if ($user_pass != $pass_confirm) {
  27. $errors[] = 'Password combination incorrect.';
  28. }
  29. $genders = array('male','female',true);
  30. if ( in_array( $gender,$genders ) === false){
  31. $errors[] = 'Invalid gender';
  32. }
  33.  
  34. //Everything has been validated, proceed with creating the user
  35.  
  36. //Create the user
  37. if(!empty($errors)):
  38. wp_send_json_error($errors);
  39. endif;
  40.  
  41. //$user_pass = wp_generate_password();
  42. $user = array(
  43. 'user_login' => $username,
  44. 'user_pass' => $user_pass,
  45. 'first_name' => $firstname,
  46. 'last_name' => $lastname,
  47. 'user_email' => $email,
  48. 'gender' => $gender
  49. );
  50. $user_id = wp_insert_user( $user );
  51.  
  52. /*Send e-mail to admin and new user -
  53. You could create your own e-mail instead of using this function*/
  54. wp_new_user_notification( $user_id, $user_pass );
  55. wp_send_json_success($user_id);
Add Comment
Please, Sign In to add comment