如何在来自 functions.php 的自定义 login.php 中显示 wp-errors

How to display wp-errors in custom login.php from functions.php

我正在尝试在 login.php 上获取 wp-errors 我确实通过在 function.php 中创建的函数在 url 中获取了错误。

这是我的 functions.php

add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect($redirect_to, $requested_redirect_to, $user) {
    $login_page  = home_url('/login/');
    if (is_wp_error($user)) {
        //Login failed, find out why...
        $error_types = array_keys($user->errors);
        if (is_array($error_types) && !empty($error_types)) {
            $error_type = $error_types[0];
        }
        wp_redirect( $login_page . "?login=failed&reason=" . $error_type ); 
        exit;
    } else {
        //Login OK - redirect to another page?
        return home_url();
    }
}

但我也希望在我的 login.php

上显示错误

首先,您需要为登录页面创建自定义模板See reference.

之后,您可以使用 $_GET['reason'] 在模板中显示原因变量值。 See reference.

你可以这样做:

<?php

if( isset( $_GET['reason'] ) ){
    $reason = $_GET['reason'];
    echo "Login error: " . $reason;
}

?>