我在wordpress主登录页面中添加了一个额外的登录字段。必须选中该复选框(以接受条款和条件),否则用户无法登录。
I have taken code from this question 这允许我在登录时添加一个自定义复选框。但是,试图修改给定的代码以验证是否选中了复选框,我无法开始工作。
function check_checkbox($user) {
$acceptance = $_POST[\'terms_acceptance\'];
$user = get_user_by(\'login\', $username );
if( !$user || !isset($acceptance) ){
remove_action(\'authenticate\', \'wp_authenticate_username_password\', 20);
$user = new WP_Error( \'denied\', __("<strong>ERROR</strong>: You\'re unique identifier was invalid.") );
}
return null;
}
add_filter( \'wp_authenticate_user\', \'check_checkbox\', 10, 3 );
我已尝试“验证”&;\'wp\\u authenticate\\u用户过滤器如图所示,但两者都不适用于我。
有人能告诉我我做错了什么吗?
最合适的回答,由SO网友:P-S 整理而成
这是完整的工作代码,请输入functions.php:
add_action(\'login_form\',\'my_added_login_field\');
function my_added_login_field(){
?>
<p>
<label for="my_extra_field">I agree to the terms<br />
<input type="checkbox" value="1" class="input" id="my_extra_field" name="my_extra_field_name"/></label>
</p>
<?php
}
function check_checkbox($user, $password)
{
if( !isset($_POST[\'my_extra_field_name\']) )
{
remove_action(\'authenticate\', \'wp_authenticate_username_password\', 20);
$user = new WP_Error( \'denied\', __("<strong>ERROR</strong>: Please agree to our terms.") );
}
return $user;
}
add_filter( \'wp_authenticate_user\', \'check_checkbox\', 10, 3 );