0

I know this has been ask a few times but I'm looking for a better way to do this. Currently, I've got it working but its not user friendly as the form will re-ask registrants to "please type your e-mail address" (even though they have already done this). This can be seen on safetyworks.com/login.

Here is the top half of my code:

<?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> 

So is there a better way to hide username? I tried using Smart WP Login but that wordpress still wants a username specified. I did also try a function similar to this but that failed.

After some digging, I found that WP-Customer Area plugin has a function that is causing this issue.

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;
        }

The problem is why does the form think the email address is blank? I tried to follow Is it possible to remove username field from the registration page? If so, how? but that causes conflicts with WP-Customer Area and won't work in a theme function file because this is read after plugins.

1 Answer 1

0

Well I was overthinking this way too much!!

All you need to do for the form is put in a dummy value for user_login so that it looks like:

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

On the top of the page I remove the $current_username = $current_email;.

On the form submission code, I make the form use $_POST['user_email'] as the user_login value.

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;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.