应该使用哪个挂钩来验证登录表单上的自定义表单域?

时间:2012-05-05 作者:greenbandit

我添加了一个自定义输入字段login_form

因此,我需要处理post数据以进行验证。我用过lostpassword_post 对于lostpassword_form 虽然效果很好,但我无法在登录表单中找到一个钩子来验证我的数据。

有什么想法吗?

2 个回复
SO网友:fuxia

使用操作\'login_init\' 捕捉到的所有呼叫wp-login.php.

示例代码:

add_action( \'login_init\', \'wpse_51227_validate_custom_field\' );

function wpse_51227_validate_custom_field()
{
    if ( ! isset ( $_POST[\'special_custom\'] )
        return;

    if ( ! is_numeric( $_POST[\'special_custom\'] )
        // handle the error
}
有两个重要的全局变量可用:

  • $errors 是的实例WP_Error. 也许你想用它来存储错误数据
  • $action 是当前操作。也许你想休息一下login 防止用户输入无效时重定向

SO网友:Giraldi

我还没有真正测试过这一点,但我只是在猜测如何使用注册&;个人资料页。

尝试以下操作:

function my_login_errors( $errors ) {
    if ( empty( $_POST[\'custom_field\'] ) )
        $errors->add( \'empty_custom_field\', \'<strong>ERROR</strong>: Please enter some info.\' );
    return $errors;
}
add_filter( \'login_errors\', \'my_login_errors\' );
Note: 修改custom_field 零件和message 照着

结束