?LOGIN=仅在某些情况下附加到URL失败

时间:2013-04-12 作者:fightstarr20

我正在使用此代码段将失败的登录重定向到我的自定义页面。。。

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\') ) {
      $pos = strpos($referrer, \'?login=failed\');
        if($pos === false) {
            // add the failed
            wp_redirect( $referrer . \'?login=failed\' );  // let\'s append some     information (login=failed) to the URL for the theme to use
        }
        else {
            // already has the failed don\'t appened it again
            wp_redirect( $referrer );  // already appeneded redirect back
        }   
      exit;
   }
}
这非常有效,但只有在用户登录过程失败的情况下,即:;他们输入了错误的用户名或密码。

如果用户执行以下任何操作,则无法正常工作。。。。

  • 不输入用户名
  • 不输入密码
  • 在任一字段中均不输入任何内容
返回的URL没有?login=failed attached to it,我想这就是代码段无法捕获它的原因。有人有什么想法吗?

3 个回复
SO网友:fightstarr20

在以下代码的帮助下解决了此问题http://css.dzone.com/articles/create-your-own-wordpress

add_action( \'authenticate\', \'pu_blank_login\');
function pu_blank_login( $user ){
    // check what page the login attempt is coming from
    $referrer = $_SERVER[\'HTTP_REFERER\'];
    $error = false;
    if($user == null || $_POST[\'pwd\'] == \'\')
    {
        $error = true;
    }
    // check that were not on the default login page
    if ( !empty($referrer) && !strstr($referrer,\'wp-login\') && !strstr($referrer,\'wp-admin\') && $error ) {
        // make sure we don\'t already have a failed login attempt
        if ( !strstr($referrer, \'?login=failed\') ) {
            // Redirect to the login page and append a querystring of login failed
            wp_redirect( $referrer . \'?login=failed_empty\' );
        } else {
            wp_redirect( $referrer );
        }
    exit;
    }
}
这与我最初发布的代码完美结合,涵盖了失败的登录尝试和密码和用户名字段为空的失败登录尝试

编辑

实际上放弃了这一点,被剪断的功能正常,但似乎阻止了我成功登录,wierd。。。。。。。。。。。

SO网友:raison

@fightstarr20差点就成功了,但正如所指出的,代码会阻止您登录。

引用的代码已在源页面(不是链接的页面)上更新。

特别是if($user==null…)应该是$\\u POST[\'log\'}=“”。。。。。

这是代码。Source Link

    add_action( \'authenticate\', \'pu_blank_login\');

    function pu_blank_login( $user ){
        // check what page the login attempt is coming from
        $referrer = $_SERVER[\'HTTP_REFERER\'];

        $error = false;

        if($_POST[\'log\'] == \'\' || $_POST[\'pwd\'] == \'\')
        {
            $error = true;
        }

        // check that were not on the default login page
        if ( !empty($referrer) && !strstr($referrer,\'wp-login\') && !strstr($referrer,\'wp-admin\') && $error ) {

            // make sure we don\'t already have a failed login attempt
            if ( !strstr($referrer, \'?login=failed\') ) {
                // Redirect to the login page and append a querystring of login failed
                wp_redirect( $referrer . \'?login=failed\' );
            } else {
                wp_redirect( $referrer );
            }

        exit;

        }
    }

SO网友:Ralf912

add_action( \'wp_authenticate\', \'my_front_end_login_fail\', 1, 2 );

function my_front_end_login_fail( $user, $pwd ) {
}
你注意到钩子了吗wp_authenticate 接受two 争论?你知道吗,你必须用add_action() 呼叫

您应该不时查看核心文件:

  $ignore_codes = array(\'empty_username\', \'empty_password\');

  if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
    do_action(\'wp_login_failed\', $username);
  }
Thewp_login_failed 仅当用户名或userpassword为not 空的

所以让我们从头开始,清理一些东西:

add_action( \'wp_authenticate\', \'my_front_end_login_fail\', 1, 2 );
add_action( \'wp_login_failed\', \'my_front_end_login_fail\', 1, 1 );

function my_front_end_login_fail( $user, $pwd = \'\' ) {

    // reasons to stop here
    // $user is not empty
    // $user is not a wp-error object
    // $pwd is not empty
    if ( ! empty( $user ) && ! empty( $pwd ) && ! is_wp_error( $user ) )
        return false;

    // if a referer is set, use it. else setup the standard login file
    $referrer = ( isset( $_SERVER[\'HTTP_REFERER\'] ) && ! empty( $_SERVER[\'HTTP_REFERER\'] ) ) ?
        $_SERVER[\'HTTP_REFERER\'] : home_url( \'wp-login.php\' ); // take the safe one, use home_url()

    /*
     * since PHP5 we can parse an url
     * @see http://php.net/manual/en/function.parse-url.php
     *
     * parse_url( \'http://www.example.com/wp-login.php?login=failed&foo=bar\' ) gives us something like that:
     *
     * array (
     *  \'scheme\' => \'http\'
     *  \'host\'   => \'www.example.com\'
     *  \'path\'   => \'/wp-login.php\'
     *  \'query\'  => \'login=failed&foo=bar\'
     *  )
     */

    $parsed_url = parse_url( $referrer );

    /*
     * Another fine function is parse_str()
     * @see: http://php.net/manual/en/function.parse-str.php
     * 
     * parse( \'login=failed&foo=bar\', $query ); results in
     * array(
     *  \'login\' => \'failed\'
     *  \'foo\'   => \'bar\'
     *  )
     *
     */
    parse_str( $parsed_url[\'query\'], $query );

    // if there\'s a valid referrer, and it\'s not the default log-in screen
    if ( ! strstr( $parsed_url[\'path\'], \'wp-login\' ) && ! strstr( $parsed_url[\'path\'], \'wp-admin\' ) ) {


        // already has the failed don\'t appened it again
        $redirect_to = $referrer;

        if( ! isset( $query[\'login\'] ) || \'failed\' !== $query[\'login\'] ) {
            // add the failed
            // but never ever use a simple string concaternation
            // what will result if the referer is \'example.com?foo=bar\'?
            // it will result in \'example.com?foo=bar?login=failed\' OUTCH!
            $redirect_to = add_query_arg( array( \'login\' => \'failed_empty\' ), $referrer );
        }

        // you don\'t want to redirect to google or somewhere else, you want to redirect to your
        // own domain. so use wp_safe_redirect()
        wp_safe_redirect( $redirect_to );
        exit;
    }

}
这有点棘手。我们将函数挂钩到两个不同的操作中。第一个动作通过two 参数,仅第二个one. 我们的回访总是需要two 参数,但如果只传递一个参数,则第二个参数将使用空字符串预定义。

注释中解释了该函数的其余部分。

结束

相关推荐

让用户从wp-login.php直接登录到站点主页,而不是仪表板

我目前正在使用一个名为Sidebar Login 它允许用户绕过仪表板直接访问站点。但是,当用户通过/wp-login.php (例如,当他们在电子邮件中单击验证链接时),他们会再次被发送到仪表板。我希望用户能够点击他们的验证链接,并将他们定向到网站主页,在那里他们可以通过我设置的侧边栏登录登录。这可能吗?