4
我在WP的注册表中只有选项的用户名,电子邮件和密码。将姓氏和名字添加到Wordpress注册表
//1. firstname
add_action('register_form','myplugin_register_form'); 功能myplugin_register_form(){
$first_name = (! empty($_POST['first_name'])) ? trim($_POST['first_name']) : '';
?>
<?php
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
function myplugin_registration_errors($errors, $sanitized_user_login, $user_email) {
if (empty($_POST['first_name']) || ! empty($_POST['first_name']) && trim($_POST['first_name']) == '') {
$errors->add('first_name_error', __('<strong>ERROR</strong>: You must include a first name.', 'mydomain'));
}
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action('user_register', 'myplugin_user_register');
function myplugin_user_register($user_id) {
if (! empty($_POST['first_name'])) {
update_user_meta($user_id, 'first_name', trim($_POST['first_name']));
}
}
在我的报名表(模板)我已经放在以下几点:
<label><?php _e('First Name', APP_TD) ?></label>
<input tabindex="3" type="text" name="first_name" class="text regular-text" id="display_name" value="<?php echo esc_attr(wp_unslash($first_name)); ?>" maxlength="100" />
和它的作品,因为它应该。它抓住名字并将其放入用户配置文件中。不过,我不知道如何添加姓以及我是一个初学者,出于某种原因,我不能得到姓氏的工作。帮助将不胜感激。
嗨,感谢您的输入。忘了提及我已经尝试了你的方法。它返回一个错误,并说,姓氏字段没有填写,即使它已经。我已将代码放入我的functions.php文件中,而其他部分放在了我的注册模板中。 – Lloyd
NVM,你的代码确实有用,谢谢!似乎我忘了更改name =“” – Lloyd
中的姓氏属性如果它工作正常,您能否将我的答案标记为已解决您的问题? :)谢谢 – Mymozaaa