Add Custom Field to Register Form

custom f

this is gonna be a quick one, cause I need to finish website. But I got this working today. This is for function.php file or in a plugin will work. This will add two forms to the register page in your wordpress, First Name and Last Name.

add_action(‘register_form’,’show_first_name_field’);
add_action(‘register_post’,’check_fields’,10,3);
add_action(‘user_register’, ‘register_extra_fields’);

function show_first_name_field(){
?>
<p>
<label>First Name<br/>
<input id=”user_email” type=”text” tabindex=”20″ size=”25″ value=”<?php echo $_POST['first']; ?>” name=”first”/>
</label>
</p>
<p>
<label>Last Name<br/>
<input id=”user_email” type=”text” tabindex=”20″ size=”25″ value=”<?php echo $_POST['last']; ?>” name=”last”/>
</label>
</p>
<?php
}

function check_fields($login, $email, $errors) {
global $firstname, $lastname;
if ($_POST['first'] == ”) {
$errors->add(‘empty_realname’, “<strong>ERROR</strong>: Please Enter in First Name”);
} else {
$firstname = $_POST['first'];
}
if ($_POST['last'] == ”) {
$errors->add(‘empty_realname’, “<strong>ERROR</strong>: Please Enter in Last Name”);
} else {
$firstname = $_POST['last'];
}
}
function register_extra_fields($user_id, $password=””, $meta=array())  {

$userdata = array();
$userdata['ID'] = $user_id;
$userdata['first_name'] = $_POST['first'];
$userdata['last_name'] = $_POST['last'];
wp_update_user($userdata);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>