Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ajax_register(){
- // First check the nonce, if it fails the function will break
- check_ajax_referer( 'ajax-register-nonce', 'security' );
- // Nonce is checked, get the POST data and sign user on
- $welcome = __("Welcome! Check your email. Processing...", "KleeiaDev");
- $user_login = $_POST['user_login'];
- $sanitized_user_login = sanitize_user( $user_login );
- $user_email = $_POST['user_email'];
- $user_pass = wp_generate_password( 12, false);
- $user_tp = $_POST['user_tp'];
- if(empty($user_tp)) $capa = 'subscriber';
- else $capa = $user_tp;
- //This is my custom function to check if my email is correctly filled in
- $extraerrors = registration_errors();
- $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email, $capa );
- $info = array();
- $info['user_login'] = $user_login;
- $info['user_password'] = $user_pass;
- $info['remember'] = true;
- //here I check If creating user gives back errors
- if (is_wp_error($user_id) or is_wp_error($extraerrors)){
- $error = $user_id->get_error_codes();
- //this is working and gives back default messages
- if(in_array('empty_user_login', $error))
- echo json_encode(array('loggedin'=>false, 'message'=>__($user_id->get_error_message('empty_user_login'))));
- elseif(in_array('existing_user_login',$error))
- echo json_encode(array('loggedin'=>false, 'message'=>__('This username is already registered.')));
- elseif(in_array('existing_user_email',$error))
- echo json_encode(array('loggedin'=>false, 'message'=>__('This email address is already registered.')));
- //this is not working and I can't display my $extraerrors
- elseif(in_array('empty_email',$extraerrors))
- echo json_encode(array('loggedin'=>false, 'message'=>__($extraerrors->get_error_message('empty_email'))));
- elseif(in_array('invalid_email',$extraerrors))
- echo json_encode(array('loggedin'=>false, 'message'=>__($extraerrors->get_error_message('invalid_email'))));
- elseif(in_array('email_exists',$extraerrors))
- echo json_encode(array('loggedin'=>false, 'message'=>__($extraerrors->get_error_message('email_exists'))));
- } else {
- $user_signon = wp_signon( $info, false );
- $user = new WP_User($user_id);
- $user->set_role($capa);
- update_user_meta( $user_id, 'user_tp', $user_tp );
- update_user_option( $user_id, 'default_password_nag', true, true );
- KleeiaDev_new_user_notification($user_id, $user_pass );
- KleeiaDev_new_user_notification_admin($user_id);
- echo json_encode(array('loggedin'=>true, 'message'=> $welcome));
- }
- die();
- }
- //this is my external function to check my email
- function registration_errors(){
- $errors = new WP_Error();
- if ( $user_email == '' ) {
- $errors->add( 'empty_email', __( '<strong>Error</strong>: Please type your e-mail address.', $current_theme_locale_name ) );
- } elseif ( ! is_email( $user_email ) ) {
- $erros->add( 'invalid_email', __( '<strong>Error</strong>: The email address isn’t correct.', $current_theme_locale_name ) );
- } elseif ( email_exists( $user_email ) ) {
- $errors->add( 'email_exists', __( '<strong>Error</strong>: This email is already registered, please choose another one.', $current_theme_locale_name ) );
- }
- if ( empty( $errors->get_error_codes() ) ) {
- return $errors;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement