Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So, I'm working on a plugin in which I'm making a custom page containing a login form as well as one for user registration; I have the forms built and operational, below is my code.

<!-- Begin login form -->               
<form name="loginform" id="login_form" class="login_form" action="<?php echo esc_url( wp_login_url() ); ?>" method="post">

    <p>
        <input type="text" name="log" id="user_login" class="input" placeholder="Username" />
    </p>

    <p>
        <input type="password" name="pwd" id="user_pass" class="input" placeholder="Password" />
    </p>

    <button name="wp-submit" id="wp-submit" class="btn"><?php _e("Sign in", "shorti"); ?></button>

    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER["REQUEST_URI"]; ?>" />

</form>
<!-- end login form -->

<!-- Begin registration form -->
<form method="post" id="register_form" class="wp-user-form" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>">

    <p><!-- Username -->
        <input type="text" name="user_login" id="user_login" class="input" placeholder="username" />
    </p>

    <p><!-- Email to send p/w to -->
        <input type="email" name="user_email" id="user_email" class="input" placeholder="email address" />
    </p>

    <p class="small-text">You will receive an email with a generated password<br />(which you can change in your "user settings")</p>

    <?php do_action('register_form'); ?>
    <button name="wp-submit" id="wp-submit" class="btn"><?php _e("Register!", "shorti"); ?></button>
    <?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?>
    <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" />
    <input type="hidden" name="user-cookie" value="1" />

</form><!-- end registration-form -->

This works fine beside the fact that whenever there's an error message it redirects the user to wp-login.php. I'm needing the page with the form to handle all the errors with AJAX; so as to give a better user experience. What is the best way to go about doing this?

share|improve this question
    
After taking a second glance at this, I've to say my duplicate suggestion is inaccurate, sorry. Besides that, what have you done so far regarding the implementation of ajax? –  ialocin Nov 10 '13 at 13:55
    
@ialocin: So far I have not done anything with AJAX. I haven't worked with AJAX much, so I don't even really know where to start. :( I've been doing some extended research over the past few days... but the only things I can find on how to override the redirection to wp-login.php require more page-loads; which is not what I want. A few of the things I found are... here and here –  Designer 17 Nov 12 '13 at 19:45
add comment

2 Answers

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/

share|improve this answer
    
Yes, the Digging into WP link is a great resource... it's what I have used to build what I currently have (the code I shared), but they don't show how to handle errors on the same page (at least I can't see where they do). Secondly, I'm building this for a custom plugin that I'm working on, which means; I really don't want to be dependent on another plugin continually working so that mine works. –  Designer 17 Nov 12 '13 at 19:51
add comment

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');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.