Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to retrieve wp error in custom login form

Tags:

php

wordpress

I have create template for login with help of wp_login_form() function. Now If user enter wrong password or username it will redirect me to the same page with argument login=failed with the following code :

add_action( 'wp_login_failed', 'front_end_login_fail' );
function front_end_login_fail( $username ) {

$_SESSION['uname'] =  $username;
// Getting URL of the login page
$referrer = $_SERVER['HTTP_REFERER'];    
$login_failed_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password' );

// 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( 93 ) . "?login=failed" ); 
    exit;
}

}

NOW this function works ok but now as per wordpress functionality which provide as follow:

1.If user enter true username but wrong password it will show error as "incorrect_password"

2.If user enter false username but true password it will show error as "invalid_username"

3.If user enter wrong username but wrong password it will show error as "invalidcombo"

Add so on please check variable $login_failed_error_codes in code... I have made some search.I got some class called "WP_error".But I dont know how it works with this code.

I am just stuck in how to pass object of WP_error from wp-login.php to my csutom template?

Thanks...any help would be appriciable.

like image 966
vrajesh Avatar asked Jun 26 '15 06:06

vrajesh


People also ask

How do I see error messages in WordPress?

Another method used to display WordPress error messages is the WP_DEBUG flag: define('WP_DEBUG', true); Just drop that line of code in your wp-config. php file and errors will start displaying.

Where can I find WP login php?

The WordPress login page can be reached by adding /login/, /admin/, or /wp-login. php at the end of your site's URL.


2 Answers

I think I understand what you are trying to achieve. You want to be able to display the reason login failed on your own custom login page. I assume you already know how to fetch the $_GET parameters, since you are using that to pass your login_failed parameter.

Use the login_redirect filter instead:

add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect($redirect_to, $requested_redirect_to, $user) {
    if (is_wp_error($user)) {
        //Login failed, find out why...
        $error_types = array_keys($user->errors);
        //Error type seems to be empty if none of the fields are filled out
        $error_type = 'both_empty';
        //Otherwise just get the first error (as far as I know there
        //will only ever be one)
        if (is_array($error_types) && !empty($error_types)) {
            $error_type = $error_types[0];
        }
        wp_redirect( get_permalink( 93 ) . "?login=failed&reason=" . $error_type ); 
        exit;
    } else {
        //Login OK - redirect to another page?
        return home_url();
    }
}
like image 117
Mikk3lRo Avatar answered Sep 18 '22 20:09

Mikk3lRo


If you have created custom template for login then Why don't you use wp_signon method with the help of custom form ?. it will return WP_error object on false, and on true it will return $user object.

<?php
if(isset($_POST['submit'])){
        $creds = array();
        $creds['user_login'] = $_POST['user_email'];
        $creds['user_password'] = $_POST['user_password'];
        $creds['remember'] = true;
        $user = wp_signon( $creds, false );
        if ( is_wp_error($user) )
            echo $user->get_error_message();
}
?>

<form id="user-credentials" method="post" action="<?php the_permalink(); ?>">
    <p><input name="user_email" type="text" placeholder="Email" /></p>
    <p><input name="user_password" type="password" placeholder="Password" /></p>
    <p><input type="submit" value="Submit" /></p>
</form>

I've not tested in but it should work.

like image 25
Touqeer Shafi Avatar answered Sep 18 '22 20:09

Touqeer Shafi