0

I have custom registration page, i want after registration redirect to home page. Via ajax i successfuly send data to backend and added user, but i have some problems with ajax, i cant get response message after sending. How i can do it ?

Then how i can add custom user meta data ( city ) ? i used function update_meta_data but not actions.

ajax-auth-script.js

jQuery(document).ready(function ($) {
    $('form#login, form#register').on('submit', function (e) {
        if (!$(this).valid()) return false;
        $('p.status', this).show().text(ajax_auth_object.loadingmessage);
        action = 'ajaxlogin';
        username =  $('form#login #username').val();
        password = $('form#login #password').val();
        email = '';
        city = '';
        role = '';
        security = $('form#login #security').val();
        if ($(this).attr('id') == 'register') {
            action = 'ajaxregister';
            username = $('#signonname').val();
            password = $('#signonpassword').val();
            email = $('#email').val();
            city = $('#signocity').val();
            role = $('#signonrole').val();
            security = $('#signonsecurity').val();
        }  
        ctrl = $(this);
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: ajax_auth_object.ajaxurl,
            data: {
                'action': action,
                'username': username,
                'password': password,
                'email': email,
                'city': city,
                'role': role,
                'security': security
            },
            success: function (data) {
                $('p.status', ctrl).text(data.message);
                console.log(data);
                if (data.loggedin == true) {
                    document.location.href = ajax_auth_object.redirecturl;
                }
            }
        });
        e.preventDefault();
    });
    

    if ($("#register").length) 
        $("#register").validate(
        { 
            rules:{
            password2:{ equalTo:'#signonpassword' 
            }   
        }}
        );
    else if ($("#login").length) 
        $("#login").validate();
});

function.php

function ajax_auth_init(){  

    wp_register_script('validate-script', get_template_directory_uri() . '/assets/js/jquery.validate.js', array('jquery') ); 
    wp_enqueue_script('validate-script');

    wp_register_script('ajax-auth-script', get_template_directory_uri() . '/assets/js/ajax-auth-script.js', array('jquery') ); 
    wp_enqueue_script('ajax-auth-script');

    wp_localize_script( 'ajax-auth-script', 'ajax_auth_object', array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'redirecturl' => home_url(),
        'loadingmessage' => __('Sending user info, please wait...')
    ));

    add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
    add_action( 'wp_ajax_nopriv_ajaxregister', 'ajax_register' );
}

if (!is_user_logged_in()) {
    add_action('init', 'ajax_auth_init');
}

function ajax_register(){

    check_ajax_referer( 'ajax-register-nonce', 'security' );

    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $city = $_POST['city'];
    $role = $_POST['role'];

    if(!empty($role) === 'Doctor') {
        $role = 'subscriber';
    } else {
        $role = 'contributor';
    }

    $user_data = array(
        'user_login' => $email,
        'user_email' => $email,
        'user_pass' => $password,
        'first_name' => $username,
        'role' => $role         
    );


    // Register the user
    $user_register = wp_insert_user( $user_data );
    if ( is_wp_error($user_register) ){ 
        $error  = $user_register->get_error_codes() ;        
        if(in_array('empty_user_login', $error))
            echo json_encode(array('loggedin'=>false, 'message'=>__($user_register->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.')));
    } else {
        echo "Something is wrong";     
    }

    die();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.