Skip to content

Instantly share code, notes, and snippets.

@noameppel
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save noameppel/d2e65367c6e1c691d0be to your computer and use it in GitHub Desktop.

Select an option

Save noameppel/d2e65367c6e1c691d0be to your computer and use it in GitHub Desktop.
Add custom fields (First name, Last name) to the WordPress registration form.
<?php
/**
* Custom Login Fields
*/
// Add a new form elements
add_action('register_form','custom_register_form');
function custom_register_form (){
$first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
$last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
$policy_checkbox = ( isset( $_POST['policy_checkbox'] ) ) ? $_POST['policy_checkbox']: ''; ?>
<p>
<label for="first_name"><?php _e('First Name','mydomain') ?><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','mydomain') ?><br />
<input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr(stripslashes($last_name)); ?>" size="25" />
</label>
</p>
<p>
<label for="policy">
<input type="checkbox" name="policy_checkbox" value="agreed" <?php if ( isset( $_POST['policy_checkbox'] ) ) { echo "checked"; } ?> >I have read and agree to the <a href="#">Policy</a>.<br>
</label>
</p><br />
<?php }
// Add validation. In this case, we make sure first_name and last_name has been provided.
add_filter('registration_errors', 'custom_registration_errors', 10, 3);
function custom_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.','mydomain') );
if ( empty( $_POST['last_name'] ) )
$errors->add( 'last_name_error', __('<strong>ERROR</strong>: You must include a last name.','mydomain') );
if ( empty( $_POST['policy_checkbox'] ) )
$errors->add( 'policy_checkbox_error', __('<strong>ERROR</strong>: You must read and agree to the Policy.','mydomain') );
return $errors;
}
// Finally, save our extra registration user meta.
add_action('user_register', 'custom_user_register');
function custom_user_register ($user_id) {
if ( isset( $_POST['first_name'] ) )
update_user_meta($user_id, 'first_name', esc_attr(stripslashes($_POST['first_name'])));
if ( isset( $_POST['last_name'] ) )
update_user_meta($user_id, 'last_name', esc_attr(stripslashes($_POST['last_name'])));
if ( isset( $_POST['policy_checkbox'] ) )
update_user_meta($user_id, 'policy_checkbox', 'Agreed');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment