WP_INSERT_USER-如何在登录前发送验证电子邮件

时间:2017-12-06 作者:Gvtha

使用下面的代码,我可以创建一个用户。但当我登录时,用户直接登录到仪表板,并且没有发送电子邮件来验证电子邮件地址。一旦wp_insert_user 函数被调用,用户必须在登录之前确认其电子邮件地址。我试过了wp_create_userregister_new_user, 但我希望功能符合我的要求。

$user_login = wp_slash( $username );
$user_email = wp_slash( $email );
$user_pass = $password;

$userdata = compact(\'user_login\', \'user_email\', \'user_pass\');
$user_id = wp_insert_user($userdata);

1 个回复
SO网友:Gvtha

1. Insert user data into wp_users table. Create a page called "Activation" or similar to it and get the ID of that page. An activation email will be sent with the activation link to the user.

function new_user($data) {

    // Separate Data
    $default_newuser = array(
        \'user_pass\' =>  wp_hash_password( $data[\'user_pass\']),
        \'user_login\' => $data[\'user_login\'],
        \'user_email\' => $data[\'user_email\'],
        \'first_name\' => $data[\'first_name\'],
        \'last_name\' => $data[\'last_name\'],
        \'role\' => \'pending\'
    );

    $user_id = wp_insert_user($default_newuser);
    if ( $user_id && !is_wp_error( $user_id ) ) {
        $code = sha1( $user_id . time() );
        $activation_link = add_query_arg( array( \'key\' => $code, \'user\' => $user_id ), get_permalink( /* YOUR ACTIVATION PAGE ID HERE */ ));
        add_user_meta( $user_id, \'has_to_be_activated\', $code, true );
        wp_mail( $data[\'user_email\'], \'ACTIVATION SUBJECT\', \'HERE IS YOUR ACTIVATION LINK: \' . $activation_link );
    }
}

2. Check activation status while logging in:

// override core function
if ( !function_exists(\'wp_authenticate\') ) :
function wp_authenticate($username, $password) {
    $username = sanitize_user($username);
    $password = trim($password);

    $user = apply_filters(\'authenticate\', null, $username, $password);

    if ( $user == null ) {
        // TODO what should the error message be? (Or would these even happen?)
        // Only needed if all authentication handlers fail to return anything.
        $user = new WP_Error(\'authentication_failed\', __(\'<strong>ERROR</strong>: Invalid username or incorrect password.\'));
    } elseif ( get_user_meta( $user->ID, \'has_to_be_activated\', true ) != false ) {
        $user = new WP_Error(\'activation_failed\', __(\'<strong>ERROR</strong>: User is not activated.\'));
    }

    $ignore_codes = array(\'empty_username\', \'empty_password\');

    if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
        do_action(\'wp_login_failed\', $username);
    }

    return $user;
}
endif;

3. Below function will get execute if the user clicks the activation link in the email.

add_action( \'template_redirect\', \'wpse8170_activate_user\' );
function wpse8170_activate_user() {
    if ( is_page() && get_the_ID() == /* YOUR ACTIVATION PAGE ID HERE */ ) {
        $user_id = filter_input( INPUT_GET, \'user\', FILTER_VALIDATE_INT, array( \'options\' => array( \'min_range\' => 1 ) ) );
        if ( $user_id ) {
            // get user meta activation hash field
            $code = get_user_meta( $user_id, \'has_to_be_activated\', true );
            if ( $code == filter_input( INPUT_GET, \'key\' ) ) {
                delete_user_meta( $user_id, \'has_to_be_activated\' );
            }
        }
    }
}
结束

相关推荐

从Functions.php访问插件数据

我正在尝试将wordpress帖子中的数据插入到新的表数据库表中,而不是WP_postmeta. 我的函数正在运行,它将插入$postid 没问题。我遇到的问题是,我需要从Marty Spellerbergs的“地址地理编码器”插件中插入纬度和经度坐标。在插件页面上,Marty说您可以使用以下内容访问循环中的Cooridate:<?php echo get_geocode_lng( $post->ID ); ?> <?php echo get_geocode_lat( $p