个性化阅读
专注于IT技术分析

如何在自定义登录表单上获取登录错误

我已经通过Elementor创建了一个自定义登录表单, 但是无法正确输出错误, 这使我的网站瘫痪了。

我做了一些PHP代码并对其进行了修改, 以尝试从WP Login表单中收集WP错误, 但是它无法正常工作。

//add hook to redirect the user back to the elementor login page if the login failed
add_action( 'wp_login_failed', 'elementor_form_login_fail' );
function elementor_form_login_fail( $username ) {
    $referrer = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
    // 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 (is_wp_error($username)) {
        //Login failed, find out why...
        $error_types = array_keys($username->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(preg_replace('/\?.*/', '', $referrer) . '?login=error&reason=' . $error_type );
        exit;
    }
}

我可以成功地测试用户已经登录, 并使用Bootstrap Alert来返回错误代码, 但是我想这样做, 所以我可以收集确切的错误。

我知道WP登录表单会执行此操作, 但是我希望此确切的错误显示在我的自定义登录名的顶部, 但是每当我在其中添加代码时, 我都会收到一条错误消息, 指出该赛特遇到了技术难题。

我的自定义登录表单位于https://www.qbeam.co.uk/login。


#1


我查看了WP错误的WordPress对象, 还查看了WP Authenticate Hook, 我设法阻止用户提交空表单, 我检查了用户名和密码是否为空, 并且不发送该表单, 然后正确提交表单后, 我将返回该表单的错误代码。

将以下代码添加到Functions.php

add_action('init', 'prevent_wp_login');

function prevent_wp_login() {
if(isset($_POST['log']) && isset($_POST['pwd']))
if($_POST['log']=='' && $_POST['pwd']=='')
{
$page = 'https://www.qbeam.co.uk/login/?login=error&reason=blank';
// Redirect to the your url
wp_redirect($page);
exit();
}
else if($_POST['log']=='')
{
$page = 'https://www.qbeam.co.uk/login/?login=error&reason=username';
// Redirect to the your url
wp_redirect($page);
exit();
}
else if($_POST['pwd']=='')
{
$page = 'https://www.qbeam.co.uk/login/?login=error&reason=password';
// Redirect to the your url
wp_redirect($page);
exit();
}
}

add_filter('login_redirect', 'qbeam_login_failed_redirect', 10, 3);
function qbeam_login_failed_redirect($user) {
$referrer = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
// 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 (is_wp_error($user)) {
//Login failed, find out why...
$error_types = array_keys($user->errors);
//Get the reason why login failed
if (is_array($error_types) && !empty($error_types)) {
$error_type = $error_types[0];
}
wp_redirect(preg_replace('/\?.*/', '', $referrer) . "?login=failed&reason=" . $error_type ); 
exit;
} 
}
}

function qbeam_login_redirect( $redirect_to, $request, $user  ) {

return ( is_array( $user->roles ) && in_array( 'administrator', $user->roles ) ) ? admin_url() : site_url() . '/my-account';
}
add_filter( 'login_redirect', 'qbeam_login_redirect', 10, 3 );

add_action('wp_logout', 'auto_redirect_external_after_logout');
function auto_redirect_external_after_logout(){
wp_redirect( 'https://www.qbeam.co.uk/login/?loggedout=true' );
exit();
}

然后在你的自定义登录表单所在的登录页面中添加与此类似的内容。

<?php

if( "error" == $_GET['login'] && "blank" == $_GET['reason'] )
{
echo 
"<div class='alert alert-warning alert-dismissible container' style='margin-top:20px; margin-bottom:20px;'>
<a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
<strong>Error!</strong> Form Cannot Be Blank, Please Try Again.
</div>";
} 

?>

然后, 你为自定义登录表单上可能遇到的每个错误制作一个标题, 然后可以使用自定义重定向来阻止所有未登录的用户访问wp-login, 可以为此或其他代码使用插件在你的Functions.php中。

希望以后能对其他人有所帮助。

赞(0)
未经允许不得转载:srcmini » 如何在自定义登录表单上获取登录错误

评论 抢沙发

评论前必须登录!