我正在尝试为我的登录页面创建安全问题字段

时间:2015-04-25 作者:023023

在过去的两个小时里,我一直在尝试创建一个mu插件,该插件要求用户首先回答一个问题,这样他就可以登录网站。

这是“拿破仑的白马叫什么名字?”,因此,用户回答“白色”,理论上应该是可行的。问题是,它似乎在登录期间没有得到验证。这是我的代码:

 <?php

add_action( \'login_form\', \'add_login_field\' );
function add_login_field() { ?>
    <p>
        <label><?php _e(\'What is the name of the white horse of Napoleon?\') ?><br />
        <input type="text" name="user_proof" id="user_proof" class="input" size="25" tabindex="20" /></label>
    </p>
<?php }

add_action( \'login_post\', \'add_login_field_validate\', 10, 3 );

function add_login_field_validate( $sanitized_user_login, $user_email, $errors) {
    if (!isset($_POST[ \'user_proof\' ]) || empty($_POST[ \'user_proof\' ])) {
        return $errors->add( \'proofempty\', \'<strong>ERROR</strong>: You did not answer the proof-of-humanship question.\'  );
    } elseif ( strtolower( $_POST[ \'user_proof\' ] ) != \'white\' ) {
        return $errors->add( \'prooffail\', \'<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.\'  );
    }
}

?>
我认为问题在于部件:

add_action( \'login_post\', \'add_login_field_validate\', 10, 3 );
有人能帮我一下吗?

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

没有login_post 我知道的操作-使用authenticate 改为过滤器,内部wp_authenticate(), 呼叫人wp_signon():

function wpse_185339_check_user_answer( $user ) {
    if ( $_SERVER[\'REQUEST_METHOD\'] === \'POST\' ) {
        if ( empty( $_POST[ \'user_proof\' ] ) ) {
            $user = new WP_Error( \'proofempty\', \'<strong>ERROR</strong>: You did not answer the proof-of-humanship question.\' );
        } elseif ( strtolower( $_POST[ \'user_proof\' ] ) !== \'white\' ) {
            $user = new WP_Error( \'prooffail\', \'<strong>ERROR</strong>: You did not answer the proof-of-humanship question correctly.\' );
        }
    }

    return $user;
}

add_filter( \'authenticate\', \'wpse_185339_check_user_answer\', 100 );

结束

相关推荐

如何在WordPress MultiSite中添加新站点和映射自定义域?

我在域上安装了wordpress多站点我将每个站点定义为一个子域例如:www.Example。com—主域www.test1。实例com---是另一个类似的站点,我有另外4/5个站点使用wordpress(WPMU)的单个安装现在我想添加一个完全限定的域,例如www.mydomain。com访问www.test1。实例com我曾尝试在我的web主机的cPannel中使用驻车域,但这是不可能的,因为子域实际上是由wordpress创建的在许多论坛中,我读到有一个插件(WordPress MU域映射),但我不