Validate Custom Login field

时间:2013-10-07 作者:Eoghan OLoughlin

我在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用户过滤器如图所示,但两者都不适用于我。

有人能告诉我我做错了什么吗?

2 个回复
最合适的回答,由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 );

SO网友:s_ha_dum

Per the Codex...

add_filter(\'wp_authenticate_user\', \'myplugin_auth_login\',10,2);
function myplugin_auth_login ($user, $password) {
     //do any extra validation stuff here
     return $user;
}
您需要返回$user 对象函数始终返回null. 这是完全未经测试的,但看起来您只需要使用return $user; 在最后一行,而不是return null;

结束

相关推荐

Wordpress custom login page

我发现了一篇关于如何创建自定义Wordpress登录页面的小文章,我想知道这种技术/实践是否安全使用?文章:http://blog.caercam.org/2012/09/21/wordpress-login-page-complete-redesign/我尝试过这个技巧,但是Logout 功能在wordpress中停止工作,如果我输入了错误的帐户用户名或密码,请单击login 它把我引向了过去login 有没有办法解决这些问题?我使用的代码与本文中的代码完全相同。