我在wp_login_form()功能的帮助下创建了登录模板.现在,如果用户输入了错误的密码或用户名,它将login=failed使用以下代码将参考页面重定向到相同的页面:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在这个功能正常,但现在按照wordpress功能提供如下:
1.如果用户输入真实用户名但密码错误,则会显示错误为"incorrect_password"
2.如果用户输入false用户名但输入真密码,则会显示错误为"invalid_username"
3.如果用户输入错误的用户名但密码错误,则会显示错误为"invalidcombo"
添加如此请检查代码中的变量 $ login_failed_error_codes ...我已经进行了一些搜索.我得到了一些名为"WP_error"的类.但我不知道它是如何工作的.
我只是陷入了如何将WP_error的对象从wp-login.php传递到我的csutom模板?
谢谢......任何帮助都会很有用.
Mik*_*lRo 11
我想我明白你想要实现的目标.您希望能够在自己的自定义登录页面上显示登录失败的原因.我假设您已经知道如何获取$_GET参数,因为您使用它来传递login_failed参数.
请改用login_redirect过滤器:
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();
}
}
Run Code Online (Sandbox Code Playgroud)