0

I'm creating a custom registration form which works using username and email inputs, but when I include First and Last name fields it won't let me register.

Here is the form:

<form id="_wb_register" action="<?php echo home_url(); ?>" method="post">

    <p>

        <label for="_wb_register_username"><?php _e('Username:', 'wb'); ?></label>
        <input type="text" name="username" id="_wb_register_username" />

    </p>

    <p>

        <label for="_wb_register_user_first_name"><?php _e('First Name:', 'wb'); ?></label>
        <input type="text" name="user_first_name" id="_wb_register_user_first_name" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" />

    </p>

    <p>

        <label for="_wb_register_user_last_name"><?php _e('Last Name:', 'wb'); ?></label>
        <input type="text" name="user_last_name" id="_wb_register_user_last_name" value="<?php echo esc_attr( wp_unslash( $last_name ) ); ?>" />

    </p>

    <p>

        <label for="_wb_register_email"><?php _e('Email:', 'wb'); ?></label>
        <input type="email" name="email" id="_wb_register_email" />

    </p>

    <div class="padding" style="height: 5px;"></div>

    <?php do_action('wb_user_panel_registration_form_bottom'); ?>

    <p><input type="submit" value="<?php _e('Sign Up', 'wb'); ?>" class="button-primary" /></p>

</form>

And here is the function used to create the user upon finishing the registration form:

add_action('wp_ajax__wb_register', '_wb_register_function');
add_action('wp_ajax_nopriv__wb_register', '_wb_register_function');

function _wb_register_function() {

    $return = array();
    $return['error'] = false;
    $return['message'] = array();

    //// VERIFIES NONCE
    $nonce = isset($_POST['nonce']) ? trim($_POST['nonce']) : '';
    if(!wp_verify_nonce($nonce, 'wb-register-nonce'))
        die('Busted!');

    //// VERIFIES CREDENTIALS
    $username = isset($_POST['username']) ? trim($_POST['username']) : '';
    $first_name = isset($_POST['user_first_name']) ? trim($_POST['user_first_name']) : '';
    $last_name = isset($_POST['user_last_name']) ? trim($_POST['user_last_name']) : '';
    $email = isset($_POST['email']) ? trim($_POST['email']) : '';

    //// MAKES SURE USER HAS FILLED USERNAME AND EMAIL
    if($email == '' || !is_email($email)) { $return['error'] = true; $return['message'] = __('Please type in a valid email address.', 'wb'); }
    if($last_name == '') { $return['error'] = true; $return['message'] = __('Please enter your last name.', 'wb'); }
    if($first_name == '') { $return['error'] = true; $return['message'] = __('Please enter your first name.', 'wb'); }
    if($username == '') { $return['error'] = true; $return['message'] = __('Please choose an username.', 'wb'); }

    ///// IF USER HAS FILLED USER, PASS, FIRST AND LAST
    if($return['error'] == false) {

        $return_registration = _wb_process_user_registration($return, $email, $username, $first_name, $last_name);
        $return = $return_registration;

    }

    echo json_encode($return);

    exit;

}

//////////////////////////////////////////////////////////////////////
///// THIS FUNCTION PROCESSES REGISTRATIONS
//////////////////////////////////////////////////////////////////////

function _wb_process_user_registration($return, $email, $username, $first_name, $last_name) {

        //// CHECKS IF USERNAME EXISTS
        $user = new WP_User('', $username);
        if(!$user->exists()) {

            //// CHECK FOR USERNAMES EMAIL
            $user = get_user_by('email', $email);
            if(!$user) {

                $password = wp_generate_password();

                //// NOW WE CAN FINALLY REGISTER THE USER
                $args = array(

                    'user_login' => esc_attr($username),
                    'first_name' => $first_name,
                    'last_name' => $last_name,
                    'user_email' => $email,
                    'user_pass' => $password,
                    'role' => 'submitter',

                );

                //// CREATES THE USER
                $user = wp_insert_user($args);

                if(!is_object($user)) {

                    //// MAKES SURE HE CAN"T SEE THE ADMIN BAR
                    update_user_meta($user, 'show_admin_bar_front', 'false');

                    $user = new WP_User($user);

When I submit the registration form it gives me the 'Please enter your first name.' error, so at least that part of the function is working. I don't understand why the First and Last names are not being registered when the Username and Email work just fine. Is there something I'm missing?

0

You're not handling the names the same way as the other fields:

//// VERIFIES CREDENTIALS
$username = isset($_POST['username']) ? trim($_POST['username']) : '';
$first_name = isset($fields['user_first_name']) ? sanitize_text_field(trim($fields['user_first_name'])) : '';
$last_name = isset($fields['user_last_name']) ? sanitize_text_field(trim($fields['user_last_name'])) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';

Notice how you use $_POST['email'] and $_POST['username'], but then you use $fields['user_last_name'], but there is no $fields variable in your code

As a sidenote, I would not use variable names such as $return, etc, they're reserved PHP keywords and using them for variable names is suboptimal and bad practice

  • Thanks Tom, originally I was handling them the same way by using $first_name = isset($_POST['user_first_name']) ? trim($_POST['user_first_name']) : ''; but I was experiencing the same issue. I just changed them back in my original post. Thanks for pointing that out. – PhillM23 Apr 25 '15 at 4:32
0
</br></br> 
<center>
    <form action=""method="POST"></br></br>   
        First Name:<input type="text" name="first_name"></br></br> 
        Last Name:<input type="text" name="last_name"></br></br>   
        <button class="" name="submit">submit</button>
    </form>
</center>

<style>
tr{border:1px solid red;}
</style>

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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