2016-01-22 70 views
0

姓名和姓氏是唯一没有保存的两个字段,其他字段保存正确。我一直在寻找一天的解决方案,我可以找到任何。我尝试另一种形式,似乎没有人工作。我真的不想使用插件。如何在前端的WordPress注册表中保存姓名和姓氏

我使用这个输入的前端

对于名称

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

在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; 
} 
+0

谷歌的Javascript会话变量 –

回答

1

使用update_user_option()add_user_meta()

在您的例子为FIRST_NAME,后 '其他' 补充一点:

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

update_user_meta($ user_ID的, 'USER_PHONE',$ USER_PHONE,$ USER_FIRST_NAME); ?? – Chris

+0

检查更新的答案。 –

+0

谢谢,它的工作增加了update_user_meta – Chris

相关问题