Advertisement
Guest User

Untitled

a guest
May 11th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. function ajax_register(){
  2.  
  3. // First check the nonce, if it fails the function will break
  4.  
  5. check_ajax_referer( 'ajax-register-nonce', 'security' );
  6. // Nonce is checked, get the POST data and sign user on
  7. $welcome = __("Welcome! Check your email. Processing...", "KleeiaDev");
  8. $user_login = $_POST['user_login'];
  9. $sanitized_user_login = sanitize_user( $user_login );
  10. $user_email = $_POST['user_email'];
  11. $user_pass = wp_generate_password( 12, false);
  12. $user_tp = $_POST['user_tp'];
  13. if(empty($user_tp)) $capa = 'subscriber';
  14. else $capa = $user_tp;
  15.  
  16. //This is my custom function to check if my email is correctly filled in
  17.  
  18. $extraerrors = registration_errors();
  19. $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email, $capa );
  20.  
  21. $info = array();
  22. $info['user_login'] = $user_login;
  23. $info['user_password'] = $user_pass;
  24. $info['remember'] = true;
  25.  
  26. //here I check If creating user gives back errors
  27.  
  28. if (is_wp_error($user_id) or is_wp_error($extraerrors)){
  29. $error = $user_id->get_error_codes();
  30.  
  31. //this is working and gives back default messages
  32.  
  33. if(in_array('empty_user_login', $error))
  34. echo json_encode(array('loggedin'=>false, 'message'=>__($user_id->get_error_message('empty_user_login'))));
  35. elseif(in_array('existing_user_login',$error))
  36. echo json_encode(array('loggedin'=>false, 'message'=>__('This username is already registered.')));
  37. elseif(in_array('existing_user_email',$error))
  38. echo json_encode(array('loggedin'=>false, 'message'=>__('This email address is already registered.')));
  39.  
  40. //this is not working and I can't display my $extraerrors
  41.  
  42. elseif(in_array('empty_email',$extraerrors))
  43. echo json_encode(array('loggedin'=>false, 'message'=>__($extraerrors->get_error_message('empty_email'))));
  44. elseif(in_array('invalid_email',$extraerrors))
  45. echo json_encode(array('loggedin'=>false, 'message'=>__($extraerrors->get_error_message('invalid_email'))));
  46. elseif(in_array('email_exists',$extraerrors))
  47. echo json_encode(array('loggedin'=>false, 'message'=>__($extraerrors->get_error_message('email_exists'))));
  48. } else {
  49. $user_signon = wp_signon( $info, false );
  50. $user = new WP_User($user_id);
  51. $user->set_role($capa);
  52. update_user_meta( $user_id, 'user_tp', $user_tp );
  53. update_user_option( $user_id, 'default_password_nag', true, true );
  54. KleeiaDev_new_user_notification($user_id, $user_pass );
  55. KleeiaDev_new_user_notification_admin($user_id);
  56. echo json_encode(array('loggedin'=>true, 'message'=> $welcome));
  57. }
  58. die();
  59. }
  60.  
  61. //this is my external function to check my email
  62.  
  63. function registration_errors(){
  64. $errors = new WP_Error();
  65. if ( $user_email == '' ) {
  66. $errors->add( 'empty_email', __( '<strong>Error</strong>: Please type your e-mail address.', $current_theme_locale_name ) );
  67. } elseif ( ! is_email( $user_email ) ) {
  68. $erros->add( 'invalid_email', __( '<strong>Error</strong>: The email address isn&#8217;t correct.', $current_theme_locale_name ) );
  69. } elseif ( email_exists( $user_email ) ) {
  70. $errors->add( 'email_exists', __( '<strong>Error</strong>: This email is already registered, please choose another one.', $current_theme_locale_name ) );
  71. }
  72. if ( empty( $errors->get_error_codes() ) ) {
  73. return $errors;
  74. }
  75. return true;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement