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?