Skip to content

Instantly share code, notes, and snippets.

@fahdi
Created December 13, 2013 10:42
Show Gist options
  • Save fahdi/7942563 to your computer and use it in GitHub Desktop.
Save fahdi/7942563 to your computer and use it in GitHub Desktop.
Add this code to your Wordpress theme's functions file. This will add two new fields to Wordpress registration form, first and last name.
<?php
//1. Add a new form element...
add_action('register_form','saratoga_register_form');
function saratoga_register_form (){
$first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
?>
<p>
<label for="first_name"><?php _e('First Name','saratoga') ?><br />
<input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr(stripslashes($first_name)); ?>" size="25" /></label>
</p>
<p>
<label for="last_name"><?php _e('Last Name','saratoga') ?><br />
<input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr(stripslashes($last_name)); ?>" size="25" /></label>
</p>
<?
}
//2. Add validation. In this case, we make sure first_name is required.
add_filter('registration_errors', 'saratoga_registration_errors', 10, 3);
function saratoga_registration_errors ($errors, $sanitized_user_login, $user_email) {
if ( empty( $_POST['first_name'] ) )
$errors->add( 'first_name_error', __('<strong>ERROR</strong>: You must include a first name.','saratoga') );
if ( empty( $_POST['last_name'] ) )
$errors->add( 'last_name_error', __('<strong>ERROR</strong>: You must include a last name.','saratoga') );
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action('user_register', 'saratoga_user_register');
function saratoga_user_register ($user_id) {
if ( isset( $_POST['first_name'] ) )
update_user_meta($user_id, 'first_name', $_POST['first_name']);
if ( isset( $_POST['last_name'] ) )
update_user_meta($user_id, 'last_name', $_POST['last_name']);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment