I've successfully created a login with Ajax, in part to this tutorial: http://natko.com/wordpress-ajax-login-without-a-plugin-the-right-way/

My problem is that it requires a redirect at the end to complete the process. Ideally, I want to remain in the same page after the login, but it looks like without the redirection, the login isn't quite complete.

If you click the logout button, you get a confirmation page, instead of just logging out.

What I'm trying to figure out is what happens during redirection that makes the logging in complete, versus just Ajax back and forth. Is it a session that gets set up? a cookie? I just feel I'm missing something.


Here's the code that does the login:

public function ajax_login(){

    // First check the nonce, if it fails the function will break
    check_ajax_referer( 'ajax-login-nonce', 'security-login' );

    // 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'=>__('Correct username or password.')));

  • Please, post the relevant code you are using in the question.
    – cybmeta
    Dec 2, 2014 at 19:29
  • 1
    presumably logging in will show you content you wouldn't see when not logged in, redirection/reload renders the page in the logged in state.
    – Milo
    Dec 2, 2014 at 19:44
  • @cybmeta I added the code that logs people in.
    – coopersita
    Dec 2, 2014 at 19:44
  • @Milo, that's exactly what I want to know. What in the reload/redirect renders the paged in the logged in state?
    – coopersita
    Dec 2, 2014 at 19:45
  • 1
    When you log in, WordPress sets an auth cookie. That cookie is sent to the server with all subsequent http requests to identify you as logged in.
    – Milo
    Dec 2, 2014 at 19:47


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.