2 comments

  1. Look at the opening form element. The value for action determines where the form data is being posted to. By changing the value you can simply tell the form to redirect to itself rather than another page.

    The codex offers an alternative approach on generating the mark-up for the necessary form:

    http://codex.wordpress.org/Customizing_the_Login_Form#Make_a_Custom_Login_Page

    Here’s a much more in-depth tutorial:

    http://digwp.com/2010/12/login-register-password-code/

    You could always just use or fork an already existing popular Ajax based log-in plugin:

    http://wordpress.org/plugins/login-with-ajax/

  2. Here is what I do. Use login form to create login https://codex.wordpress.org/Function_Reference/wp_login_form

    Below are the login logout redirects and login error handling. This will help you with login issue.

    /** Login Redirect
     * Redirect user after successful login.
     *
     * @param string $redirect_to URL to redirect to.
     * @param string $request URL the user is coming from.
     * @param object $user Logged user's data.
     * @return string
     */
    function my_login_redirect( $redirect_to, $request, $user ) {
        //is there a user to check?
        global $user;
        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
            //check for admins
            if ( in_array( 'administrator', $user->roles ) ) {
                // redirect them to the default place
                return home_url('/wp-admin/');
            } else {
                return home_url();
            }
        } else {
            return $redirect_to;
        }
    }
    
    /*Login Error Handle*/
    add_action( 'wp_login_failed', 'aa_login_failed' ); // hook failed login
    
    function aa_login_failed( $user ) {
        // check what page the login attempt is coming from
        $referrer = $_SERVER['HTTP_REFERER'];
    
        // check that were not on the default login page
        if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') && $user!=null ) {
            // make sure we don't already have a failed login attempt
            if ( !strstr($referrer, '?login=failed' )) {
                // Redirect to the login page and append a querystring of login failed
                wp_redirect( $referrer . '?login=failed');
            } else {
                wp_redirect( $referrer );
            }
    
            exit;
        }
    }
    
    /*Login Empty Fields Error handling*/
    add_action( 'authenticate', 'pu_blank_login');
    
    function pu_blank_login( $user ){
        // check what page the login attempt is coming from
        $referrer = $_SERVER['HTTP_REFERER'];
    
        $error = false;
    
        if($_POST['log'] == '' || $_POST['pwd'] == '')
        {
            $error = true;
        }
    
        // check that were not on the default login page
        if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') && $error ) {
    
            // make sure we don't already have a failed login attempt
            if ( !strstr($referrer, '?login=failed') ) {
                // Redirect to the login page and append a querystring of login failed
                wp_redirect( $referrer . '?login=failed' );
            } else {
                wp_redirect( $referrer );
            }
    
        exit;
    
        }
    }
    
    /*Logout Redirect*/
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
    
    function go_home(){
      wp_redirect( home_url('/login/') );
      exit();
    }
    add_action('wp_logout','go_home');
    

Comments are closed.