输入错误密码后如何重定向用户?


16

wp_login_form()用来在jQuery对话框窗口中显示登录表单。

如果用户输入了错误的密码,则该用户将被带到后端。我不要 有没有一种方法可以通知用户他输入了错误的密码并且仍然保留在同一页面上?

wp_login_form()我来之前,我正在使用一个插件。我希望我可以避免为此使用插件。

我的代码:

wp_login_form( array(
  'label_remember' => __( 'Remember me' ),
  'label_log_in' => __( 'Login' )
) );

Answers:


5

wp_login_form()创建一个动作属性为的表单site_url/wp-login.php,这意味着当您单击“提交”按钮时,该表单将被发布到site_url/wp-login.php该表单,该表单将忽略由于错误(例如错误的密码)而导致的redirect_to,因此在您的情况下,请返回使用插件或重新创建整个登录过程这样您就可以控制错误了,看看自定义登录表单上的检查用户名是否正确,这是一个非常相似的问题。


26

我来自Google。但是答案并不令我满意。我找了一段时间,找到了一个更好的解决方案。

将此添加到您的functions.php

add_action( 'wp_login_failed', 'my_front_end_login_fail' );  // hook failed login

function my_front_end_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') ) {
      wp_redirect( $referrer . '?login=failed' );  // let's append some information (login=failed) to the URL for the theme to use
      exit;
   }
}

谢谢Alexey,我将对此进行测试(因为我仍在使用插件)
Steven

7
当输入错误的凭据时,Alexey的解决方案有效,但不幸的是,当用户忘记输入用户名或密码时,Alexey的解决方案将失败。显然,在这种情况下,Wordpress甚至不会尝试登录用户,因此不会执行wp_login_failed操作。
SzczepanHołyszewski2012年

我会在这里使用wp_get_referer()节省一些时间:codex.wordpress.org/Function_Reference/wp_get_referer
杰克

1
另外,您肯定在这里使用add_query_arg,因此wp_redirect应该为:“ wp_redirect(add_query_arg('login','failed',$ referrer));”;
杰克

18

我正在使用的当前方法来处理此处概述的所有问题,即使使用空白的用户名/密码也可以很好地工作,并且不依赖javascript(尽管js可能会很好)。

add_action( 'wp_login_failed', 'custom_login_failed' );
function custom_login_failed( $username )
{
    $referrer = wp_get_referer();

    if ( $referrer && ! strstr($referrer, 'wp-login') && ! strstr($referrer,'wp-admin') )
    {
        wp_redirect( add_query_arg('login', 'failed', $referrer) );
        exit;
    }
}

关键是此过滤器可更改如何处理空白的用户名/密码:

add_filter( 'authenticate', 'custom_authenticate_username_password', 30, 3);
function custom_authenticate_username_password( $user, $username, $password )
{
    if ( is_a($user, 'WP_User') ) { return $user; }

    if ( empty($username) || empty($password) )
    {
        $error = new WP_Error();
        $user  = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));

        return $error;
    }
}

通过将用户重定向到您的自定义登录页面,并将该页面用于login_failed重定向,您可以更进一步并完全替换wp-login.php。完整代码:

/**
 * Custom Login Page Actions
 */
// Change the login url sitewide to the custom login page
add_filter( 'login_url', 'custom_login_url', 10, 2 );
// Redirects wp-login to custom login with some custom error query vars when needed
add_action( 'login_head', 'custom_redirect_login', 10, 2 );
// Updates login failed to send user back to the custom form with a query var
add_action( 'wp_login_failed', 'custom_login_failed', 10, 2 );
// Updates authentication to return an error when one field or both are blank
add_filter( 'authenticate', 'custom_authenticate_username_password', 30, 3);
// Automatically adds the login form to "login" page
add_filter( 'the_content', 'custom_login_form_to_login_page' );

/**
 * Custom Login Page Functions
 */
function custom_login_url( $login_url='', $redirect='' )
{
    $page = get_page_by_path('login');
    if ( $page )
    {
        $login_url = get_permalink($page->ID);

        if (! empty($redirect) )
            $login_url = add_query_arg('redirect_to', urlencode($redirect), $login_url);
    }
    return $login_url;
}
function custom_redirect_login( $redirect_to='', $request='' )
{
    if ( 'wp-login.php' == $GLOBALS['pagenow'] )
    {
        $redirect_url = custom_login_url();

        if (! empty($_GET['action']) )
        {
            if ( 'lostpassword' == $_GET['action'] )
            {
                return;
            }
            elseif ( 'register' == $_GET['action'] )
            {
                $register_page = get_page_by_path('register');
                $redirect_url = get_permalink($register_page->ID);
            }
        }
        elseif (! empty($_GET['loggedout'])  )
        {
            $redirect_url = add_query_arg('action', 'loggedout', custom_login_url());
        }

        wp_redirect( $redirect_url );
        exit;
    }
}
function custom_login_failed( $username )
{
    $referrer = wp_get_referer();

    if ( $referrer && ! strstr($referrer, 'wp-login') && ! strstr($referrer, 'wp-admin') )
    {
        if ( empty($_GET['loggedout']) )
        wp_redirect( add_query_arg('action', 'failed', custom_login_url()) );
        else
        wp_redirect( add_query_arg('action', 'loggedout', custom_login_url()) );
        exit;
    }
}
function custom_authenticate_username_password( $user, $username, $password )
{
    if ( is_a($user, 'WP_User') ) { return $user; }

    if ( empty($username) || empty($password) )
    {
        $error = new WP_Error();
        $user  = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));

        return $error;
    }
}
function custom_login_form_to_login_page( $content )
{
    if ( is_page('login') && in_the_loop() )
    {
        $output = $message = "";
        if (! empty($_GET['action']) )
        {
            if ( 'failed' == $_GET['action'] )
                $message = "There was a problem with your username or password.";
            elseif ( 'loggedout' == $_GET['action'] )
                $message = "You are now logged out.";
            elseif ( 'recovered' == $_GET['action'] )
                $message = "Check your e-mail for the confirmation link.";
        }

        if ( $message ) $output .= '<div class="message"><p>'. $message .'</p></div>';
        $output .= wp_login_form('echo=0&redirect='. site_url());
        $output .= '<a href="'. wp_lostpassword_url( add_query_arg('action', 'recovered', get_permalink()) ) .'" title="Recover Lost Password">Lost Password?</a>';

        $content .= $output;
    }
    return $content;
}

自定义并添加以下内容,以将徽标添加到wp-login页面以恢复密码:

// calling it only on the login page
add_action( 'login_enqueue_scripts', 'custom_login_css', 10 );
function custom_login_css() { wp_enqueue_style( 'custom_login_css', get_template_directory_uri() .'/library/css/login.css', false ); }
// changing the logo link from wordpress.org to your site
add_filter( 'login_headerurl', 'custom_login_logo_url' );
function custom_login_logo_url() { return home_url(); }
// changing the alt text on the logo to show your site name
add_filter( 'login_headertitle', 'custom_login_title' );
function custom_login_title() { return get_option('blogname'); }

登录徽标CSS:

.login h1 a {
    background: url(../images/login-logo.png) no-repeat top center;
    width: 274px;
    height: 63px;
    text-indent: -9999px;
    overflow: hidden;
    padding-bottom: 15px;
    display: block;
}

编辑:我只是在另一个站点上从头开始实现了这一点,发现上面的“更进一步”更加完整,并修复了“ add_actions”中的小语法错误。添加了一些注释和一种自动将登录表单添加到登录页面而无需单独的模板文件的方法。登录表单方法应该在大多数情况下都可以使用,因为它附加在“ the_content”上,如果登录页面上有多个循环,则可能会引起问题,请在这种情况下使用page-login.php模板。


1
谢谢,我将对此进行研究(看看是否可以使其与Twitter和FB等第三方登录一起使用)
Steven

1
杰克-这是一个很好的完整解决方案。感谢分享。+1
henrywright

基本上,它被包装到名为Sewn In Template Login的插件中,这是一个非常完整的小型插件。wordpress.org/plugins/sewn-in-template-login
Jake

很好,唯一的问题是它实际上并没有在前端抛出错误
。...– Siyah,

4

对于SzczepanHołyszewski关于已接受解决方案中的空字段的观点的解决方案,以下jQuery将阻止进入标准wp-login页面:(添加到登录页面模板或footer.php)

jQuery("#loginform-custom").submit(function(){
     var isFormValid = true;
       jQuery("input").each(function()
       {
       if (jQuery.trim($(this).val()).length == 0){
       jQuery(this).addClass("submit_error");
       isFormValid = false;
       }     
     else {
     jQuery(this).removeClass("submit_error");
     }
     });
     return isFormValid;
});

0

阿列克谢答案的另一项补充。您可以添加jquery函数以检查字段之一是否为空。这样,除非进行检查,否则表单不会提交,从而阻止WordPress重定向到/wp-login.php。

  <script>
        $("#wp-submit").click(function() {
          var user = $("input#user_login").val();
            if (user == "") {
            $("input#user_login").focus();
            return false;
          }
         });
  </script>   

仍不确定如何解决忘记密码的问题


3
请注意,WordPress以“无冲突”模式加载jQuery。该$别名不工作。
s_ha_dum

您还必须考虑用户点击[输入],而不是单击登录按钮。另外,您还需要检查空白密码。
史蒂文

-1
jQuery("#loginform-custom").submit(function(){
     var isFormValid = true;
       jQuery("input").each(function()
       {
       if (jQuery.trim($(this).val()).length == 0){
       jQuery(this).addClass("submit_error");
       isFormValid = false;
       }     
     else {
     jQuery(this).removeClass("submit_error");
     }
     });
     return isFormValid;
});

编辑您的答案,并添加解释:为什么可以解决问题?
fuxia
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.