我如何确定用户在注册后已经验证了他们的电子邮件?

时间:2016-07-09 作者:Simon Cossar

我正在开发一个插件,它使用WordPress作为另一个应用程序的单一登录提供者。我需要确保用户已通过回复由发送的电子邮件来验证其电子邮件地址wp_new_user_notification().

到目前为止,我发现最好的方法是after_password_reset 操作并添加user\\u元数据以指示电子邮件已验证。这是可行的,但它真正告诉我的是reset_password 函数已调用。

有没有更好的方法?

2 个回复
SO网友:Simon Cossar

我尝试了几种不同的方法来验证用户的电子邮件。现在,我要做的是:

当用户首次注册时,将用户的user\\u元数据“email\\u not\\u verified”设置为1。

add_action( \'user_register\', \'sc_user_email_not_verified\' );
function sc_user_email_not_verified( $user_id ) {
  update_user_meta( $user_id, \'email_not_verified\', 1 );
}
然后,覆盖wp_new_user_notification 功能,以便将“email\\u verification\\u key”添加到登录url。它还将该密钥另存为user\\u元数据。

function wp_new_user_notification( $user_id, $depreciated = null, $notify = \'\' ) {

...

$email_verification_key = wp_generate_password( 20, false );
update_user_meta( $user_id, \'email_verification_key\', $email_verification_key );

$message = sprintf(__(\'Username: %s\'), $user->user_login) . "\\r\\n\\r\\n";
$message .= __(\'To set your password, visit the following address:\') . "\\r\\n\\r\\n";
$message .= \'<\' . network_site_url("wp-login.php?action=rp&key=$key&mail_key=$email_verification_key&login=" . rawurlencode($user->user_login), \'login\') . ">\\r\\n\\r\\n";
$message .= wp_login_url() . "\\r\\n";

wp_mail($user->user_email, sprintf(__(\'[%s] Your username and password info\'), $blogname), $message);
}
然后,挂接“validate\\u password\\u reset”操作,检查密码重置请求中的电子邮件验证密钥是否与保存的密钥匹配。如果密钥不匹配,请删除该用户并将其重定向回注册表,错误为“emailnotverified”。如果密钥确实匹配,请删除“email\\u not\\u verified”元数据。

add_action( \'validate_password_reset\', \'sc_verify_user_email\', 10, 2 );
function sc_verify_user_email( $errors, $user ) {
    if ( isset( $_REQUEST[\'mail_key\'] ) ) {
        $email_verification_key = $_REQUEST[\'mail_key\'];
        $saved_key              = get_user_meta( $user->ID, \'email_verification_key\', true );

        if ( ! ( $email_verification_key === $saved_key ) ) {
            require_once( ABSPATH . \'wp-admin/includes/user.php\' );
            wp_delete_user( $user->ID );
            wp_redirect( network_site_url( "wp-login.php?action=register&error=emailnotverified" ) );
            exit;
        } else {
            delete_user_meta( $user->ID, \'email_not_verified\' );
        }
    }
}
如果电子邮件未经验证,请添加一条消息,当出现“emailnotverified”错误时,该消息将显示在注册页面上。

add_filter( \'login_message\', \'sc_email_not_verified_message\' );
function sc_email_not_verified_message() {
    $action = isset( $_REQUEST[\'action\'] ) ? $_REQUEST[\'action\'] : \'\';
    $error = isset( $_REQUEST[\'error\'] ) ? $_REQUEST[\'error\'] : \'\';

    if ( \'register\' === $action && \'emailnotverified\' === $error ) {
        $message = \'<p class="message">\' . __( \'Your email address could not be verified. Please try registering again.\' ) . \'</p>\';
        return $message;
    }
}
在单点登录功能中,如果用户具有“email\\u not\\u verified”元数据,则不要将其登录到客户端应用程序。(如果用户是通过插件添加的注册表创建的,则可能会发生这种情况。)

$current_user = wp_get_current_user();
if ( get_user_meta( $current_user->ID, \'email_not_verified\', true ) ) {
    echo( \'Invalid request.\' ); // This needs to be changed to a redirect.
    exit;
}
我还添加了一个复选框,用于在“用户编辑”页面上显示和覆盖用户的电子邮件验证状态。

编辑:

钩住validate_password_reset 行动可能不是最好的方式。它在重置密码之前调用,因此即使密码重置过程中有错误(例如,如果密钥过期或无效),也会验证电子邮件

一个更好的方法似乎是resetpass_form 操作并向保存“mail\\u key”值的密码重置表单添加隐藏字段:

add_action( \'resetpass_form\' \'sc_mail_key_field\' );
function sc_mail_key_field() {

    if ( isset( $_REQUEST[\'mail_key\'] ) ) { 

        $mail_key = sanitize_key( wp_unslash( $_REQUEST[\'mail_key\'] ) );
        wp_nonce_field( \'verify_email\', \'verify_email_nonce\' );
        echo \'<input type="hidden" name="mail_key" value="\' . esc_attr( $mail_key ) . \'" />\';
    }
}
然后可以钩住after_password_reset 根据$_POST[\'mail_key\'] 价值

可以在此处找到一个示例插件:email address verification plugin

SO网友:birgire

我查了一下wp_usermeta 并注意到default_password_nag 元键。

我查过了,这是在#9710 大约7年前。

如果用户有一个自动生成的密码,那么它的值是1,她将在仪表板屏幕上显示一个通知。

当她第一次注册时,default_password_nag 是1,当她在电子邮件链接后重置密码时,密码变为0。

如果它在您的设置中有用的话,您可以进一步研究它。

否则,可以创建一个自定义计数器或标志,并将其存储在用户元中。E、 g.钩住用户创建过程,然后在用户重置密码或登录时更新它。

相关推荐