how to retrieve wp error in custom login form

2019-04-04 04:57发布

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.

标签: php wordpress
4条回答
神经病院院长
2楼-- · 2019-04-04 05:37

Looks like following answer is what you need:

You need to hook into the authenticate wordpress hook. Then return a new WP_Error object to generate an error message and redirect back to the login page. Here is an example.

add_filter('authenticate', 'check_login_submit', 40, 3);

function check_login_submit($user, $username, $password) {
    $WP_Error = new WP_Error();
    $WP_Error->add('my_error', '<strong>Error</strong>: Something went wrong.');
    return $WP_Error;
}
查看更多
做自己的国王
3楼-- · 2019-04-04 05:38

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.

查看更多
4楼-- · 2019-04-04 05:57
function front_end_login_fail( $username ) {
 $set_confirm=0;
$_SESSION['uname'] =  $username;
/*******Check whether user entered username or email to login*********/
if(is_email( $username ) ){
       if( email_exists( $username )) {
           $uid = email_exists( $username );
           $confirm_mail =get_user_meta($uid,'confirm_mail',true);
           if($confirm_mail!=1){
                $set_confirm=1;
            }
       }
    //$user_check = get_user_by( 'email', $username );
    //print_r($user_check);

}else{

    if ( username_exists( $username ) ){
         $uid = username_exists( $username );
           $confirm_mail =get_user_meta($uid,'confirm_mail',true);
          if($confirm_mail!=1){
               $set_confirm=1;
            }       
    }
}
    //$user_check = get_user_by( 'user_login ', $username );
    //print_r($user_check);

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

}
add_action( 'wp_login_failed', 'front_end_login_fail' );
查看更多
等我变得足够好
5楼-- · 2019-04-04 06:01

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();
    }
}
查看更多
登录 后发表回答