Chris
Chris

Reputation: 35

How can I save the name and last name in a WordPress registration form in front end

The name and last name is the only two field that aren't saving, the other ones save correctly. I been looking for a solution for a day and I ca'n find any. I try another form and no one seem to be working. I really don't want to use a pluging.

I'm using this input at the front end

For Name

<input type="text" name="first_name" id="f_name" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name'];?>" class="form-control input-lg" placeholder="First Name" tabindex="1"> 

for Last name

<input type="text" name="last_name" id="l_name" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name'];?>" class="form-control input-lg" placeholder="Last Name" tabindex="2">

In Function.php

add_action('template_redirect', 'register_user');
function register_user(){
if(isset($_GET['do']) && $_GET['do'] == 'register'):
  $errors = array();
if(empty($_POST['user'])) 
   $errors[] = 'Please enter a username.<br>';
if(empty($_POST['email'])) 
   $errors[] = 'Please enter a email.<br>';
if(empty($_POST['pass'])) 
   $errors[] = 'Please enter a password.<br>';
if(empty($_POST['cpass'])) 
   $errors[] = 'Please enter a confirm password.<br>';
if((!empty($_POST['cpass']) && !empty($_POST['pass'])) && ($_POST['pass'] != $_POST['cpass'])) 
   $errors[] = 'Entered password did not match.';
$user_first_name = esc_attr($_POST['first_name']);
$user_last_name  = esc_attr($_POST['last_name']);
$user_login      = esc_attr($_POST['user']);
$user_email      = esc_attr($_POST['email']);
$user_pass       = esc_attr($_POST['pass']);
$user_confirm_pass = esc_attr($_POST['cpass']);
$user_phone      = esc_attr($_POST['phone']);
$sanitized_user_login = sanitize_user($user_login);
$user_email      = apply_filters('user_registration_email', $user_email);

if(!is_email($user_email)) 
   $errors[] = 'Invalid e-mail.<br>';
elseif(email_exists($user_email)) 
   $errors[] = 'This email is already registered.<br>';

if(empty($sanitized_user_login) || !validate_username($user_login)) 
   $errors[] = 'Invalid user name. Try again; usernames can contain only letters, hyphens and underscores.<br>';
elseif(username_exists($sanitized_user_login)) 
   $errors[] = 'User name already exists.<br>';

if(empty($errors)):
  $user_id = wp_create_user($sanitized_user_login, $user_pass, $user_email);

if(!$user_id):
  $errors[] = 'Registration failed';
else:
  update_user_option($user_id, 'default_password_nag', true, true);
  wp_new_user_notification($user_id, $user_pass);
  update_user_meta ($user_id, 'user_phone', $user_phone);
  wp_cache_delete ($user_id, 'users');
  wp_cache_delete ($user_login, 'userlogins');
  do_action ('user_register', $user_id);
  $user_data = get_userdata ($user_id);
  if ($user_data !== false)  {
     wp_clear_auth_cookie();
     wp_set_auth_cookie ($user_data->ID, true);
     do_action ('wp_login', $user_data->user_login, $user_data);
     // Redirect user.
      wp_redirect( get_bloginfo('url') . '/edit-profile/');
     exit();
   }
  endif;
endif;
if(!empty($errors)) 
  define('REGISTRATION_ERROR', serialize($errors));
endif;
}

Upvotes: 0

Views: 706

Answers (1)

Loai Abdelhalim
Loai Abdelhalim

Reputation: 1967

Use update_user_option() or add_user_meta().

In your example for first_name, add this after 'else':

add_user_meta($user_id, 'first_name', $user_first_name);

Upvotes: 1

Related Questions