简体   繁体   中英

WP admin ajax return 404

I want create custom login page. I have some problems with ajax-admin php. When i send data for loggined i get response 0, and 404 error. Also when i try get success response data similarly give null. Where my mistake ?

My main task after successful authorization was redirected to the main page and if unsuccess authorization give error text

login.php

        <form id="login" action="login" method="post" >         
            <p class="status"></p>
            <div class="form-group">
                <label>Email</label>
                <input class="form-control required" id="logusername" type="email" name="logusername">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input class="form-control required" id="logpassword" type="password" name="logpassword" >
            </div>
            <a class="forgot-pass" href="<?php echo wp_lostpassword_url(); ?>">Forgot Password</a>
            <div class="d-flex justify-content-center mt-3">
                <input type="submit" class="btn btn-success submit_button" value="Login">
                <?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?>
            </div>
        </form> 

ajax-login-script.js

jQuery(document).ready(function($) {
    $('form#login').on('submit', function(e){
        $('form#login p.status').show().text(ajax_login_object.loadingmessage);
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: ajax_login_object.ajaxurl,
            data: { 
                'action': 'ajax_login',
                'username': $('form#login #logusername').val(), 
                'password': $('form#login #logpassword').val(), 
                'security': $('form#login #security').val() },
            success: function(data){
                console.log(data); // null
                $('form#login p.status').text(data.message);
                if (data.loggedin == true){
                    document.location.href = ajax_login_object.redirecturl;
                } else {
                    console.log(false);
                }
            }
        });
        e.preventDefault();
    });

});

function.php

function ajax_login_init(){

    wp_register_script('ajax-login-script', get_template_directory_uri() . '/ajax-login-script.js', array('jquery'), time() ); 
    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...!')
    ));

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

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

function ajax_login(){

    check_ajax_referer( 'ajax-login-nonce', 'security' );
    $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) ){
        wp_set_current_user($user_signon->ID);
        wp_set_auth_cookie($user_signon->ID);
        echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...')));
    }
    
    die();
}

You must have to pass correct hook name after, The Hook must be end with the name of action you are passing through jQuery request ex. wp_ajax_nopriv_{action_name} so please correct it first.

add_action( 'wp_ajax_nopriv_ajax_login', 'ajax_login' );

Put this action outside the function and do your login condition inside the hook callback function.

function ajax_login(){   

    if (!is_user_logged_in()) { // It's not necessary it should be based on your requirement

        // Your code goes here.
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM