Require Additional Profile Fields at Registration

<?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 );
}

7 thoughts on “Require Additional Profile Fields at Registration”

  1. What way would you recommend adding some new meta data in this fashion? Here you’re just updating the meta, but what if you wanted some new fields to be added to the user profile?

  2. Thanks, (very useful as well) but I don’t have any issues adding the fields to the profile page.

    What I meant is within the initial user registration form above. I suppose the best way is to just use add_user_meta within the “insert_register_fields” method. 🙂

    1. You can use add_ as well. I tend to use update_ since it will add the key/value pair if the key does not already exist.

  3. I try this code on a website on worpress multisite, it’s ok but the validate field function on the hook ‘register_post’ don’t works.
    have you an idea?

Leave a Reply to dcolumbus Cancel reply

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

%d bloggers like this: