<?php
add_action( 'register_form', 'additional_profile_fields' );
function additional_profile_fields() { ?>
<p>
<label><?php _e('First Name') ?><br />
<input type="text" name="first_name" id="first_name" class="input" size="25" tabindex="20" /></label>
</p>
<p>
<label><?php _e('Last Name') ?><br />
<input type="text" name="last_name" id="last_name" class="input" size="25" tabindex="20" /></label>
</p>
<?php }
add_action( 'register_post', 'add_register_field_validate_first_name', 10, 3 );
function add_register_field_validate_first_name( $sanitized_user_login, $user_email, $errors) {
if (!isset($_POST[ 'first_name' ]) || empty($_POST[ 'first_name' ])) {
return $errors->add( 'firstnameempty', '<strong>ERROR</strong>: Please provide a first name.' );
}
}
add_action( 'register_post', 'add_register_field_validate_last_name', 10, 3 );
function add_register_field_validate_last_name( $sanitized_user_login, $user_email, $errors) {
if (!isset($_POST[ 'last_name' ]) || empty($_POST[ 'last_name' ])) {
return $errors->add( 'lastnameempty', '<strong>ERROR</strong>: Please provide a last name.' );
}
}
add_action( 'user_register', 'insert_register_fields' );
function insert_register_fields( $user_id ) {
$first_name = apply_filters('pre_user_first_name', $_POST['first_name']);
$last_name = apply_filters('pre_user_last_name', $_POST['last_name']);
update_user_meta( $user_id, 'first_name', $first_name );
update_user_meta( $user_id, 'last_name', $last_name );
}
Function: update_user_meta
Add Fields to Profile/Edit-User Pages
Are the built in profile fields not as extensive as you’d like? That can be fixed! The following code will add a field (“greeting”) to the “Personal Options” section as well as another (“phone number”) to the “Contact Info” section. Of course, neither of those would be useful if the data wasn’t saved, so that’s in the code too.
add_action( 'personal_options_update', 'save_custom_profile_fields' ); add_action( 'edit_user_profile_update', 'save_custom_profile_fields' ); function save_custom_profile_fields( $user_id ) { update_user_meta( $user_id, 'phone_number', $_POST['phone_number'], get_user_meta( $user_id, 'phone_number', true ) ); update_user_meta( $user_id, 'greeting', $_POST['greeting'], get_user_meta( $user_id, 'greeting', true ) ); } add_filter( 'user_contactmethods', 'add_contact_option', 10, 2 ); function add_contact_option( $user_contactmethods, $user ) { $user_contactmethods['phone_number'] = 'Phone Number'; return $user_contactmethods; } add_action( 'personal_options', 'add_profile_options'); function add_profile_options( $profileuser ) { $greeting = get_user_meta($profileuser->ID, 'greeting', true); ?><tr> <th scope="row">Greeting</th> <td><input type="text" name="greeting" value="<?php echo $greeting; ?>" /></td> </tr><?php }