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.

My custom login form redirects to the wp-admin login when an user doesn't manage to guesses his/her credentials right. How can I manage to redirect the user to the current form instead? And of course show the proper errors?

Here is the code: (same as on WordPress manual)

$args = array(
        'echo' => true,
        'redirect' => site_url( $_SERVER['REQUEST_URI'] ), 
        'form_id' => 'loginform',
        'label_username' => __( 'Username' ),
        'label_password' => __( 'Password' ),
        'label_remember' => __( 'Remember Me' ),
        'label_log_in' => __( 'Log In' ),
        'id_username' => 'user_login',
        'id_password' => 'user_pass',
        'id_remember' => 'rememberme',
        'id_submit' => 'wp-submit',
        'remember' => true,
        'value_username' => NULL,
        'value_remember' => false ); 

wp_login_form( $args );
share|improve this question
    
Did you tried echoing the site_url($_SERVER['REQUEST_URI'] ), because it works fine, I've used it succesfuly ? –  sven Oct 21 '13 at 14:20
1  
Take a look at this question: wordpress.stackexchange.com/questions/15633/… –  ialocin Oct 21 '13 at 16:16
add comment

2 Answers

up vote 1 down vote accepted
+50

While coding a custom login page, one needs to take care of the errors, else WordPress would redirect the page to /wp-admin or wp-login.php on wrong passwords or empty credentials. Please follow the below to solve this issue:--

Please use the below in your theme's functions.php file:-

/**
 * Function Name: front_end_login_fail.
 * Description: This redirects the failed login to the custom login page instead of default login page with a modified url
**/
add_action( 'wp_login_failed', 'front_end_login_fail' );
function front_end_login_fail( $username ) {

// Getting URL of the login page
$referrer = $_SERVER['HTTP_REFERER'];    
// if there's a valid referrer, and it's not the default log-in screen
if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) {
    wp_redirect( get_permalink( LOGIN_PAGE_ID ) . "?login=failed" ); 
    exit;
}

}

/**
 * Function Name: check_username_password.
 * Description: This redirects to the custom login page if user name or password is   empty with a modified url
**/
add_action( 'authenticate', 'check_username_password', 1, 3);
function check_username_password( $login, $username, $password ) {

// Getting URL of the login page
$referrer = $_SERVER['HTTP_REFERER'];

// if there's a valid referrer, and it's not the default log-in screen
if( !empty( $referrer ) && !strstr( $referrer,'wp-login' ) && !strstr( $referrer,'wp-admin' ) ) { 
    if( $username == "" || $password == "" ){
        wp_redirect( get_permalink( LOGIN_PAGE_ID ) . "?login=empty" );
        exit;
    }
}

}
// Replace my constant 'LOGIN_PAGE_ID' with your custom login page id.

and the below in the custom login page file to show the corresponding errors:--

<div class="wp_login_error">
    <?php if( isset( $_GET['login'] ) && $_GET['login'] == 'failed' ) { ?>
        <p>The password you entered is incorrect, Please try again.</p>
    <?php } 
    else if( isset( $_GET['login'] ) && $_GET['login'] == 'empty' ) { ?>
        <p>Please enter both username and password.</p>
    <?php } ?>
</div>          
share|improve this answer
1  
Missing ) at wp_redirect( get_permalink( LOGIN_PAGE_ID ) . "?login=failed"; –  Roel Veldhuizen Oct 23 '13 at 9:04
    
thanks for pointing that out. :) –  Maruti Mohanty Oct 23 '13 at 9:33
add comment

Did you tried echoing the

site_url($_SERVER['REQUEST_URI'] );

this one works fine, I've used it succesfuly

'redirect' => get_permalink()

Update For redirect on login failure, you gotta use wp_login_failed hook, and some custom code to redirect user to desires page with proper warning message.

add_action('wp_login_failed', '_login_failed_redirect');

function _login_failed_redirect( $username ){

  //get your page by slug and then its permalink
  $post = get_page_by_path('slug');

  //Or you can get your page ID, if you are assigning a custom template to a page.
  $redirect_page = !empty ( $post ) ? get_permalink ( $post->ID ) : site_url();

  $user = get_user_by('login', $username );

  if(!$user){
    //Username incorrect
    wp_redirect($redirect_page .'?login_error=1');

  }else{
   //Username Password combination incoorect
    wp_redirect($redirect_page .'?login_error=2');
  }

}

And if you are redirecting to some template, you can easily get login_error and display it accordingly.

share|improve this answer
    
User is still redirected to /wp-admin when typing a wrong password. Redirect is only for redirection after login. –  Roel Veldhuizen Oct 21 '13 at 14:44
    
Yes I didn't saw it, will give you an update in 15 mins. –  sven Oct 21 '13 at 16:57
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.