允许用户在注册期间设置密码

时间:2017-03-02 作者:Nitish Paswan

目前在我的网站上进行每一次新的注册。新注册用户正在收到他们的凭据,即电子邮件中的用户名和密码。

我想允许用户在注册时设置密码,然后将电子邮件验证链接发送到他们的电子邮件id。

下面是我要修改的代码:

<form method="post" class="wp-user-form " id="wp_signup_form_\' . $rand_id . \'" enctype="multipart/form-data">

            <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">\';
    $cs_opt_array = array(
        \'id\' => \'\',
        \'std\' => \'\',
        \'cust_id\' => \'user_login_\' . $rand_id,
        \'cust_name\' => \'user_login\' . $rand_id,
        \'classes\' => \'form-control\',
        \'extra_atr\' => \' size="20" tabindex="101" placeholder="\' . __(\'Username*\', \'theme_domain\') . \'"\',
        \'return\' => true,
    );
    $output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);

    $output .= \'</div>\';
    $output .=$cs_form_fields_frontend->cs_form_text_render(
            array(\'name\' => __(\'Email*\', \'theme_domain\'),
                \'id\' => \'user_email\' . $rand_id . \'\',
                \'classes\' => \'col-md-12 col-lg-12 col-sm-12 col-xs-12\',
                \'std\' => \'\',
                \'description\' => \'\',
                \'return\' => true,
                \'hint\' => \'\'
            )
    );
$output .=$cs_form_fields_frontend->cs_form_hidden_render(
            array(\'name\' => __(\'Post Type\', \'theme_domain\'),
                \'id\' => \'user_role_type\' . $rand_id . \'\',
                \'classes\' => \'col-md-12 col-lg-12 col-sm-12 col-xs-12\',
                \'std\' => \'candidate\',
                \'description\' => \'\',
                \'return\' => true,
                \'hint\' => \'\'
            )
    );
if (is_user_logged_in()) {
        $output .= \'<div class="col-md-6 col-lg-6 col-sm-12 col-xs-12">\';

        $cs_opt_array = array(
            \'id\' => \'\',
            \'std\' => __(\'Create Account\', \'theme_domain\'),
            \'cust_id\' => \'submitbtn\' . $rand_id,
            \'cust_name\' => \'user-submit\',
            \'cust_type\' => \'button\',
            \'extra_atr\' => \' tabindex="103" onclick="javascript:show_alert_msg(\\\'\' . __("Please logout first then try to registration again", "theme_domain") . \'\\\')"\',
            \'classes\' => \'cs-bgcolor user-submit  acc-submit\',
            \'return\' => true,
        );
        $output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);

        $output .= \'
        <!--</div>-->
        </div>\';
    } else {
        $output .= \'<div class="col-md-6 col-lg-6 col-sm-12 col-xs-12">\';

        $cs_opt_array = array(
            \'id\' => \'\',
            \'std\' => __(\'Create Account\', \'theme_domain\'),
            \'cust_id\' => \'submitbtn\' . $rand_id,
            \'cust_name\' => \'user-submit\',
            \'cust_type\' => \'button\',
            \'extra_atr\' => \' tabindex="103" onclick="javascript:cs_registration_validation(\\\'\' . admin_url("admin-ajax.php") . \'\\\',\\\'\' . $rand_id . \'\\\')"\',
            \'classes\' => \'cs-bgcolor user-submit  acc-submit\',
            \'return\' => true,
        );
$output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);

        $cs_opt_array = array(
            \'id\' => \'\',
            \'std\' => $role,
            \'cust_id\' => \'login-role\',
            \'cust_name\' => \'role\',
            \'cust_type\' => \'hidden\',
            \'return\' => true,
        );
        $output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);

        $cs_opt_array = array(
            \'id\' => \'\',
            \'std\' => \'cs_registration_validation\',
            \'cust_name\' => \'action\',
            \'cust_type\' => \'hidden\',
            \'return\' => true,
        );
$output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);

        $output .= \'
                    </div>
                    \';
    }

    $output .=\'
        <div class="col-md-6 col-lg-6 col-sm-12 col-xs-12 login-section">
                        <i class="icon-user-add"></i> \' . __("Already have an account?", "theme_domain") . \'
                        <a href="#" class="login-link-page">\' . __(\'Login Now\', \'theme_domain\') . \'</a>
                    </div>
                </div>
                </div>
                <div id="result_\' . $rand_id . \'" class="status-message"><p class="status"></p></div>
                </div>\';
    $output .=\'</form>\';
    $output .=\'</div>\';
下面是我目前在我的网站上使用的功能

function mytheme_registration_save($user_id) {


if ( isset($_REQUEST[\'action\']) && $_REQUEST[\'action\'] == \'register\' ) {

        $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
        wp_set_password($random_password, $user_id);
        $reg_user = get_user_by(\'ID\', $user_id);
        if ( isset($reg_user->roles) && (in_array(\'subscriber\', $reg_user->roles) || in_array(\'editor\', $reg_user->roles) || in_array(\'author\', $reg_user->roles)) ) {
            // Site owner email hook
            do_action(\'theme_domain_new_user_notification_site_owner\', $reg_user->data->user_login, $reg_user->data->user_email);
            // normal user email hook
            do_action(\'theme_domain_user_register\', $reg_user, $random_password);
        }
    }
}
}

2 个回复
最合适的回答,由SO网友:Fernando Baltazar 整理而成

好吧,你可以创建自己的登录表单,这也是我几年前做的,但使用插件更容易做到。有很多这样的插件,例如:Auto login new user https://wordpress.org/plugins/auto-login-new-user-after-registration/; 您还可以添加social login 对于您的Wordpress,这将在您的站点上创建自动注册。

SO网友:Nitish Paswan

为了添加自定义密码而不是生成默认密码,我使用以下代码将密码字段添加到表单中:

//Add Custom Password Field
        $output .= \'<div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">\';
        $output .=\'<label class="password">\';

        $cs_opt_array = array(
            \'id\' => \'\',
            \'std\' => __(\'Password\', \'jobhunt\'),
            \'cust_id\' => \'user_pass\' . $rand_id,
            \'cust_name\' => \'pass1\',
            \'cust_type\' => \'password\',
            \'classes\' => \'form-control\',
            \'extra_atr\' => \' size="100" tabindex="101" placeholder="\' . __(\'Password*\', \'jobhunt\') . \'"\',
            \'return\' => true,


        );
        $output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);

        $output .=\'</label>\';

        $output .= \'</div>\';
        $output .= \'<div class="col-md-12 col-lg-12 col-sm-12 col-xs-12">\';
        $output .=\'<label class="password">\';

        $cs_opt_array = array(
            \'id\' => \'\',
            \'std\' => __(\'Password\', \'jobhunt\'),
            \'cust_id\' => \'user_pass\' . $rand_id,
            \'cust_name\' => \'pass2\',
            \'cust_type\' => \'password\',
            \'classes\' => \'form-control\',
            \'extra_atr\' => \' size="100" tabindex="101" placeholder="\' . __(\'Repeat Password*\', \'jobhunt\') . \'"\',
            \'return\' => true,
        );
        $output .= $cs_form_fields2->cs_form_text_render($cs_opt_array);

        $output .=\'</label>\';
        $output .= \'</div>\';
        //End
之后,我通过以下代码验证这两个字段:

 if ( empty( $_POST[\'pass1\'] ) || ! empty( $_POST[\'pass1\'] ) && trim( $_POST[\'pass1\'] ) == \'\' ) {
        $json[\'type\'] = "error";
        $json[\'message\'] = $cs_danger_html . __(\'<strong>ERROR</strong>: Password field is required.\') . $cs_msg_html;
        echo json_encode($json);
        exit();
        }
        if ( empty( $_POST[\'pass2\'] ) || ! empty( $_POST[\'pass2\'] ) && trim( $_POST[\'pass2\'] ) == \'\' ) {
         $json[\'type\'] = "error";
         $json[\'message\'] = $cs_danger_html . __(\'<strong>ERROR</strong>: Confirm Password field is required.\') . $cs_msg_html;
         echo json_encode($json);
         exit();
        }
        if ( $_POST[\'pass1\'] != $_POST[\'pass2\'] ) {
            $json[\'type\'] = "error";
            $json[\'message\'] = $cs_danger_html . __(\'<strong>ERROR</strong>: Password field and Confirm Password field do not match.\') . $cs_msg_html;
            echo json_encode($json);
            exit();
        }

    $random_password = $_POST[\'pass1\'];
验证后,将数据保存到数据库中,我使用以下代码保存输入的密码:

function mytheme_registration_save($user_id) {


if ( isset($_REQUEST[\'action\']) && $_REQUEST[\'action\'] == \'register\' ) {

    $random_password = $_POST[\'pass1\'];
    wp_set_password($random_password, $user_id);
    $reg_user = get_user_by(\'ID\', $user_id);
    if ( isset($reg_user->roles) && (in_array(\'subscriber\', $reg_user->roles) || in_array(\'editor\', $reg_user->roles) || in_array(\'author\', $reg_user->roles)) ) {
        // Site owner email hook
        do_action(\'theme_domain_new_user_notification_site_owner\', $reg_user->data->user_login, $reg_user->data->user_email);
        // normal user email hook
        do_action(\'theme_domain_user_register\', $reg_user, $random_password);
    }
}
}
}
现在,为了发送验证电子邮件,我找到了以下链接。这对我真的很有帮助。Email Verification Link 非常感谢Clay 电子邮件验证帖子的作者。

我只是简单地使用了以下主题的功能。php

function wc_registration_redirect( $redirect_to ) {
wp_logout();
wp_redirect( \'/sign-in/?q=\');
exit;
}
// when user login, we will check whether this guy email is verify
function wp_authenticate_user( $userdata ) {
$isActivated = get_user_meta($userdata->ID, \'is_activated\', true);
if ( !$isActivated ) {
    $userdata = new WP_Error(
        \'inkfool_confirmation_error\',
        __( \'<strong>ERROR:</strong> Your account has to be activated before you can login. You can resend by clicking <a href="/sign-in/?u=\'.$userdata->ID.\'">here</a>\', \'inkfool\' )
    );
}
return $userdata;
    }
// when a user register we need to send them an email to verify their account
function my_user_register($user_id) {
    // get user data
    $user_info = get_userdata($user_id);
    // create md5 code to verify later
    $code = md5(time());
    // make it into a code to send it to user via email
    $string = array(\'id\'=>$user_id, \'code\'=>$code);
    // create the activation code and activation status
    update_user_meta($user_id, \'is_activated\', 0);
    update_user_meta($user_id, \'activationcode\', $code);
    // create the url
    $url = get_site_url(). \'/sign-in/?p=\' .base64_encode( serialize($string));
    // basically we will edit here to make this nicer
    $html = \'Please click the following links <br/><br/> <a href="\'.$url.\'">\'.$url.\'</a>\';
    // send an email out to user
    wc_mail($user_info->user_email, __(\'Please activate your account\'), $html);
}
// we need this to handle all the getty hacks i made
function my_init(){
    // check whether we get the activation message
    if(isset($_GET[\'p\'])){
        $data = unserialize(base64_decode($_GET[\'p\']));
        $code = get_user_meta($data[\'id\'], \'activationcode\', true);
        // check whether the code given is the same as ours
        if($code == $data[\'code\']){
            // update the db on the activation process
            update_user_meta($data[\'id\'], \'is_activated\', 1);
            wc_add_notice( __( \'<strong>Success:</strong> Your account has been activated! \', \'inkfool\' )  );
        }else{
            wc_add_notice( __( \'<strong>Error:</strong> Activation fails, please contact our administrator. \', \'inkfool\' )  );
        }
    }
    if(isset($_GET[\'q\'])){
        wc_add_notice( __( \'<strong>Error:</strong> Your account has to be activated before you can login. Please check your email.\', \'inkfool\' ) );
    }
    if(isset($_GET[\'u\'])){
        my_user_register($_GET[\'u\']);
        wc_add_notice( __( \'<strong>Succes:</strong> Your activation email has been resend. Please check your email.\', \'inkfool\' ) );
    }
}
// hooks handler
add_action( \'init\', \'my_init\' );
add_filter(\'woocommerce_registration_redirect\', \'wc_registration_redirect\');
add_filter(\'wp_authenticate_user\', \'wp_authenticate_user\',10,2);
add_action(\'user_register\', \'my_user_register\',10,2);

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register