如何在自定义登录表单中检索 wp 错误 [英] how to retrieve wp error in custom login form
问题描述
我在 wp_login_form()
函数的帮助下创建了登录模板.现在,如果用户输入错误的密码或用户名,它将使用以下代码将我重定向到带有参数 login=failed
的同一页面:
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;
}
}
现在这个功能可以正常工作,但现在根据 wordpress 功能提供如下:
NOW this function works ok but now as per wordpress functionality which provide as follow:
1.如果用户输入真实用户名但错误密码,则会显示错误为incorrect_password"
1.If user enter true username but wrong password it will show error as "incorrect_password"
2.如果用户输入false用户名但true密码,则会显示错误为"invalid_username"
2.If user enter false username but true password it will show error as "invalid_username"
3.如果用户输入错误用户名但错误密码,将显示错误为"invalidcombo"
3.If user enter wrong username but wrong password it will show error as "invalidcombo"
添加等请检查代码中的变量$login_failed_error_codes...我进行了一些搜索.我得到了一个名为WP_error"的类.但我不知道它是如何处理这段代码的.
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.
我只是被困在如何将 WP_error 的对象从 wp-login.php 传递到我的 csutom 模板?
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.
推荐答案
我想我明白你想要达到的目标.您希望能够在您自己的自定义登录页面上显示登录失败的原因.我假设您已经知道如何获取 $_GET
参数,因为您正在使用它来传递您的 login_failed
参数.
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.
改用 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();
}
}
这篇关于如何在自定义登录表单中检索 wp 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!