如何在WordPress注册时隐藏用户名?

时间:2017-06-20 作者:timrosenthal

我知道这已经被问了好几次了,但我正在寻找更好的方法来做到这一点。目前,我已经可以使用了,但它对用户不友好,因为表单会再次要求注册者“请键入您的电子邮件地址”(即使他们已经这样做了)。这可以在上看到safetyworks.com/login.

下面是我的代码的上半部分:

<?php
$current_email = isset($_POST[\'user_email\']) ? $_POST[\'user_email\'] : \'\';
$current_username = $current_email;

?>

<?php if ($this->should_print_form()) : ?>

    <?php $this->print_form_header(); ?>

    <div class="row clearfix">
        <div class="col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2">
            <div class="panel panel-primary panel-border top">
                <div class="panel-heading">
                    <span class="panel-title fs-lg"><i class="fa fa-edit"></i> <?php _e(\'Create an account\', \'cuarlf\'); ?></span>
                </div>

                <div class="panel-body">
                    <div class="form-group mb-lg">
                        <label for="user_email" class="control-label top-label"><?php _e(\'Email Address\', \'cuarlf\'); ?></label>
                        <div class="row clearfix">
                            <div class="col-xs-12">
                                <span class="append-icon right"><i class="field-icon fa fa-asterisk text-muted"></i></span>
                                <input class="form-control" type="email" name="user_email" id="user_email" value="<?php echo esc_attr($current_email); ?>">
                               <input class="form-control" type="hidden" name="user_login" id="user_login" value="<?php echo $current_username; ?>"> 
                                        </div>
                        </div>
                    </div> 
那么,有没有更好的方法来隐藏用户名?我尝试使用智能WP登录,但wordpress仍然需要指定用户名。我也尝试过类似的函数,但失败了。

经过一些挖掘,我发现WP Customer Area插件有一个导致此问题的功能。

public static function register_new_user($user_login, $user_email)
        {
            global $wpdb;

            $errors = new WP_Error();

            $sanitized_user_login = sanitize_user($user_login);
            $user_email = apply_filters(\'user_registration_email\', $user_email);

            // Check the username
            if ($sanitized_user_login == \'\')
            {
                $errors->add(\'empty_username\', __(\'Please enter a username.\', \'cuarlf\'));
            }
            elseif ( !validate_username($user_login))
            {
                $errors->add(\'invalid_username\', __(\'Sorry. Please enter a username using only lowercase letters and numbers.\', \'cuarlf\'));
                $sanitized_user_login = \'\';
            }
            elseif (username_exists($sanitized_user_login))
            {
                $errors->add(\'username_exists\', __(\'This username is already registered. Please choose another one.\', \'cuarlf\'));
            }

            // Check the email address
            if ($user_email == \'\')
            {
                $errors->add(\'empty_email\', __(\'Please type your email address.\', \'cuarlf\'));
            }
            elseif ( !is_email($user_email))
            {
                $errors->add(\'invalid_email\', __(\'The email address isn&#8217;t correct.\', \'cuarlf\'));
                $user_email = \'\';
            }
            elseif (email_exists($user_email))
            {
                $errors->add(\'email_exists\', __(\'This email is already registered, please choose another one.\', \'cuarlf\'));
            }

            do_action(\'register_post\', $sanitized_user_login, $user_email, $errors);

            $errors = apply_filters(\'registration_errors\', $errors, $sanitized_user_login, $user_email);

            if ($errors->get_error_code())
            {
                return $errors;
            }

            $user_pass = wp_generate_password(12, false);
            $user_id = wp_create_user($sanitized_user_login, $user_pass, $user_email);
            if ( !$user_id)
            {
                $errors->add(\'registerfail\',
                    sprintf(__(\'Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !\', \'cuarlf\'), get_option(\'admin_email\')));

                return $errors;
            }

            update_user_option($user_id, \'default_password_nag\', true, true); //Set up the Password change nag.


            // Generate something random for a key...
            $activation_key = wp_generate_password(20, false);
            do_action(\'retrieve_password_key\', $user_login, $activation_key);

            // Now insert the new md5 key into the db
            $wpdb->update($wpdb->users, array(\'user_activation_key\' => $activation_key), array(\'user_login\' => $user_login));

            // Send notifications
            $user = get_userdata($user_id);
            self::new_user_notification_admin($user);
            self::new_user_notification($user, $activation_key);

            return $user_id;
        }
问题是为什么表单认为电子邮件地址是空的?我试着跟着Is it possible to remove username field from the registration page? If so, how? 但这会导致与WP客户区冲突,并且不会在主题函数文件中工作,因为这是在插件之后读取的。

1 个回复
SO网友:timrosenthal

我想得太多了!!

表单所需做的一切就是为user\\u login输入一个伪值,使其看起来像:

<input class="form-control" type="hidden" name="user_login" id="user_login" value="ABC">

在页面顶部,我删除了$current_username = $current_email;.

在表单提交代码上,我使用$_POST[\'user_email\'] 作为user\\u登录值。

if (isset($_POST[\'user_email\']))
            {

                $user_email = $_POST[\'user_email\'];
        $user_login = $_POST[\'user_email\'];

                // Check captch if enabled
                if (class_exists("ReallySimpleCaptcha"))
                {
                    $captcha_instance = new ReallySimpleCaptcha();

                    if ( !isset($_POST[\'cuar_captcha_prefix\']) || !isset($_POST[\'captcha\']))
                    {
                        $this->form_errors[] = new WP_Error(\'captcha\',
                            __("You must write the code displayed in the image above.", \'cuarlf\'));

                        return;
                    }
                    else
                    {
                        $prefix = $_POST[\'cuar_captcha_prefix\'];
                        $code = $_POST[\'captcha\'];

                        $captcha_checked = $captcha_instance->check($prefix, $code);
                        $captcha_instance->remove($prefix);
                        $captcha_instance->cleanup();

                        if ( !$captcha_checked)
                        {
                            $this->form_errors[] = new WP_Error(\'captcha\', __("The code you entered is not correct.", \'cuarlf\'));

                            return;
                        }
                    }
                }

                $errors = CUAR_WPLoginHelper::register_new_user($user_login, $user_email);
                if (is_wp_error($errors))
                {
                    $this->form_errors[] = $errors;

                    return;
                }
            }
            else
            {
                $this->form_errors[] = new WP_Error(\'user_login\', __("You must enter a valid username and a valid email address.", \'cuarlf\'));

                return;
            }

            $lf_addon = $this->plugin->get_addon(\'login-forms\');
            $this->form_messages[] = sprintf(__(\'An email has been sent with the instructions to activate your account. \'
                . \'You can then go to the <a href="%1$s" class="alert-link">login page</a>\', \'cuarlf\'), $lf_addon->get_login_url());
            $this->should_print_form = false;
        }

结束

相关推荐