如何将姓名保存在前端的WordPress注册表中


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

姓名是唯一两个未保存的字段,其他字段保存正确。我一直在寻找一天的解决方案,但我找不到任何解决方案。我尝试另一种形式,但似乎没有人在工作。我真的不想使用插件。

我在前端使用此输入

对于名称

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

对于姓氏

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

在功能上.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;
}

使用 update_user_option() 或 add_user_meta()。

在first_name示例中,在"else"之后添加以下内容:

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