Home > OS >  Login Form - How to check if the username or POST password field is empty and show an error text?
Login Form - How to check if the username or POST password field is empty and show an error text?

Time:08-12

I built a custom ajax login form for my wordpress website, everything works fine. What I'm trying to do now is check if the user has left the username and password fields with no values and show an appropriate error message.

Currently, if you enter the wrong username and password, the message "Wrong username or password" is displayed

Similarly, if you leave username and password blank, the same message is displayed, while I would like to display something like: "you must enter username and password to log in".

So here's what I'm trying to do...

/* Ajax Form Login - Function 1 */
function ajax_login_init(){
 
    wp_register_script('ajax-login-script', get_template_directory_uri() . '/ajax-login-script.js', array('jquery') ); 
    wp_enqueue_script('ajax-login-script');

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

    // Enable the user with no privileges to run ajax_login() in AJAX
    add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}

// Execute the action only if the user isn't logged in
if (!is_user_logged_in()) {
    add_action('init', 'ajax_login_init');
}

/* Ajax Form Login - Function 2 */
function ajax_login() {
    // First check the nonce, if it fails the function will break
    check_ajax_referer( 'ajax-login-nonce', 'security' );

    // Nonce is checked, get the POST data and sign user on
    $info = array();
    $info['user_login'] = $_POST['username'];
    $info['user_password'] = $_POST['password'];
    $info['remember'] = true;

    $user_signon = wp_signon( $info, false );
    
    if ( is_wp_error($user_signon) ){
        echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.')));
    } else {
        echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...')));
    }
    
    // This is what I am trying to integrate
    // Check if either the username value or POST password value is empty
    if( empty($_POST['username']) || empty($_POST['password']) ) {
        echo json_encode(array('loggedin'=>false, 'message'=>__('you must enter username and password to log in.')));
    }

    die();
}

This is what I am trying to integrate

The problem is that when I insert this piece of code, if I click on the login button, I get a block on loading with the message "Sending user info, please wait ..."

Can anyone tell me what I'm doing wrong? I appreciate any help, thanks for any replies.

// Check if either the username value or POST password value is empty
    if( empty($_POST['username']) || empty($_POST['password']) ) {
        echo json_encode(array('loggedin'=>false, 'message'=>__('you must enter username and password to log in.')));
    }

CodePudding user response:

Thanks to Ritesh Khandekar's comment I was able to achieve my goal. It works correctly but I don't know if it's the right way. Anyway I leave the code below for anyone who needs it or would like to make improvements in a more practical way.

// Check if either the username value or POST password value is empty
    if( empty($_POST['username']) || empty($_POST['password']) ) {
        echo json_encode(array('loggedin'=>false, 'message'=>__('you must enter username and password to log in.')));
    }  elseif ( is_wp_error($user_signon) ){
        echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.')));
    }  else {
        echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...')));
    }
  • Related