在wordpress注册上隐藏用户名

时间:2017-06-20 18:13:40

标签: php wordpress

我正在寻找一种更好的方法来隐藏wordpress注册表单上的用户名。目前,我已经有了它的工作,但它不是用户友好的,因为表格将重新询问注册人“请输入您的电子邮件地址”(即使他们已经这样做了)。这可以在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> 

那么有更好的隐藏用户名的方法吗?我尝试使用Smart WP Login,但wordpress仍然需要指定用户名。我也尝试了类似于Smart WP Login承诺但失败的效果的功能。

我发现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;
        }

问题是为什么表单认为电子邮件地址是空白的?

1 个答案:

答案 0 :(得分:0)

嗯,我太过于过分思考!!

您需要为表单做的所有事情都放在user_login的虚拟值中,以便它看起来像:

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

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

在表单提交代码中,我使用$_POST['user_email']作为user_login值。

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;
        }
相关问题