如何在我的WordPress登录表单页面上验证额外的PIN字段?

时间:2018-12-14 作者: Victor

我已经能够在wp\\u登录中使用以下代码向WordPress登录表单添加一个额外字段。php文件,感谢StackExchange。

add_action(\'login_form\',\'my_added_login_field\');
function my_added_login_field(){
    //Output your HTML
?>
    <p>
        <label for="my_extra_field">Pin<br>
        <input type="text" tabindex="20" size="20" value="" class="input" id="my_extra_field" name="my_extra_field_name"></label>
    </p>
<?php
}
我需要帮助来验证/连接新的自定义字段到wp\\u用户中名为serials的数据列。我希望用户能够登录,如果他们的用户名、密码和数据库中的新序列号正确/存在。

1 个回复
SO网友:butlerblog

我假设您所说的“我需要帮助来验证/连接新的自定义字段到中的数据列wp_users 你的意思是“连续剧”wp_usermeta 元键为“serials”的字段。

要验证作为登录的一部分的任何其他字段,请使用wp_authenticate_user filter.

首先检查$user 是一个WP错误,如果是,由于登录失败,因此没有必要继续。如果不是错误,请继续检查自定义字段是否为空,因为您需要确保它已填写。如果字段为空,则返回错误。

最后,如果它是一个有效的用户,并且字段中给出了值,则检索db值(如上所述,假设这是wp\\U usermeta中的用户元键,因为wp\\U users中没有自定义字段)。如果比较值失败,则返回错误。

add_filter( \'wp_authenticate_user\', \'my_validate_pin\', 10, 2 );
function my_validate_pin( $user, $password ) {

    // Validate PIN if we\'re not already in an error.
    if ( ! is_wp_error( $user ) ) {
        $pin = get_user_meta( $user->ID, \'serials\', true );

        // Error if field is empty.
        if ( ! isset( $_POST[\'my_extra_field_name\'] ) ) {
            remove_action( \'authenticate\', \'wp_authenticate_username_password\', 20 );
            $user = new WP_Error( \'failed\', __("<strong>ERROR</strong>: Must include PIN") );
        }

        // Assuming you\'re looking for form value to match the db value.
        if ( $pin != $_POST[\'my_extra_field_name\'] ) {
            // If values don\'t match, return error
            remove_action( \'authenticate\', \'wp_authenticate_username_password\', 20 );
            $user = new WP_Error( \'failed\', __("<strong>ERROR</strong>: Invalid PIN") );
        }

    }

    return $user;
}

相关推荐

Wp-login.php?reDirect_to=HTTPS问题

wp登录。php?redirect\\u to=https问题如果我在错误的地方发帖,请原谅我,但我在这里是想看看你是否能在你的时间里帮我几分钟。问题是,一个网站在wp登录时显示出持续的内部错误。php。我能够登录并访问前端,没有任何困难。我尝试过调试,但错误没有显示在任何页面上。我测试了主题、插件等,etcI检查了htaccess文件是否存在损坏(没有损坏),我将htaccess文件设置为调试和显示,但没有显示任何内容。我确实检查了服务器端的调试文件,它显示了一个插件的一些错误,当我停用该插件时,内部错