Advertisement
Guest User

Untitled

a guest
May 10th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. function ajax_register(){
  2. // First check the nonce, if it fails the function will break
  3. check_ajax_referer( 'ajax-register-nonce', 'security' );
  4. // Nonce is checked, get the POST data and sign user on
  5. $welcome = __("Welcome! Check your email. Processing...", "KleeiaDev");
  6. $user_login = $_POST['user_login'];
  7. $sanitized_user_login = sanitize_user( $user_login );
  8. $user_email = $_POST['user_email'];
  9. $user_pass = wp_generate_password( 12, false);
  10. $user_tp = $_POST['user_tp'];
  11. if(empty($user_tp)) $capa = 'subscriber';
  12. else $capa = $user_tp;
  13. $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email, $capa );
  14. //login next
  15. $info = array();
  16. $info['user_login'] = $user_login;
  17. $info['user_password'] = $user_pass;
  18. $info['remember'] = true;
  19. $user_signon = wp_signon( $info, false );
  20. if (is_wp_error($user_id)){
  21. //for sure something wrong here
  22. $errors = new WP_Error();
  23. if ( $sanitized_user_login == '' ) {
  24. $errors->add( 'empty_username', __( '<strong>Error</strong>: Please enter a username.', $current_theme_locale_name ) );
  25. } elseif ( ! validate_username( $user_login ) ) {
  26. $errors->add( 'invalid_username', __( '<strong>Error</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.', $current_theme_locale_name ) );
  27. $sanitized_user_login = '';
  28. } elseif ( username_exists( $sanitized_user_login ) ) {
  29. $errors->add( 'username_exists', __( '<strong>Error</strong>: This username is already registered, please choose another one.', $current_theme_locale_name ) );
  30. }
  31. if ( $user_email == '' ) {
  32. $errors->add( 'empty_email', __( '<strong>Error</strong>: Please type your e-mail address.', $current_theme_locale_name ) );
  33. } elseif ( ! is_email( $user_email ) ) {
  34. $errors->add( 'invalid_email', __( '<strong>Error</strong>: The email address isn&#8217;t correct.', $current_theme_locale_name ) );
  35. $user_email = '';
  36. } elseif ( email_exists( $user_email ) ) {
  37. $errors->add( 'email_exists', __( '<strong>Error</strong>: This email is already registered, please choose another one.', $current_theme_locale_name ) );
  38. }
  39. echo json_encode(array('loggedin'=>false, 'message' => $errors));
  40. } else {
  41. $user = new WP_User($user_id);
  42. $user->set_role($capa);
  43. update_user_meta( $user_id, 'user_tp', $user_tp );
  44. update_user_option( $user_id, 'default_password_nag', true, true );
  45. KleeiaDev_new_user_notification($user_id, $user_pass );
  46. KleeiaDev_new_user_notification_admin($user_id);
  47. //Signon next
  48. echo json_encode(array('loggedin'=>true, 'message'=> $welcome));
  49. }
  50. die();
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement