在过去的两个小时里,我一直在尝试创建一个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 );
有人能帮我一下吗?
最合适的回答,由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 );