I’m currently working on an application that requires users to register before logging into the site. The application is based on WordPress, but the registration process is using a custom template and requires some extra information.

As an example, I thought I’d share how I’m programmatically creating a user during the registration process, adding extra information to the user’s profile, and then displaying it in the user management dashboard.

Simplified Requirements

Add Custom User Meta During Registration

Examples like this have potential to be lengthy, so in order to keep in simple, let’s set the following requirements. I’m asking the user for:

  • First Name
  • Last Name
  • Email Address
  • Zip Code

WordPress natively supports fields for everything except a zip code so that’ll be the field that we use to add custom user meta.

Also note that there’s a lot of validation that occurs before successfully creating a user. You should always account for empty strings, well-formed email addresses, well-formed zip codes, and so on all of which are outside the scope of this particular post.

Secondly, this assumes that proper sanitization has been performed on each of the input fields. Nothing should ever be serialized to the database without being sanitized.

Add Custom User Meta During Registration

Assuming that all of the fields are valid (such as in the image above), you can begin programmatically creating the user.

Given that the input is stored in an array called $input, you can use the wp_insert_user function. Note that this function returns the ID of the user once it has been created:

$user_id = wp_insert_user(
		'user_login'	=>	$input['email_address'],
		'user_pass'	=>	wp_generate_password ( 12, false ),
		'first_name'	=>	$input['first_name'],
		'last_name'	=>	$input['last_name'],
		'user_email'	=>	$input['email_address'],
		'display_name'	=>	$input['first_name'] . ' ' . $input['last_name'],
		'nickname'	=>	$input['first_name'] . ' ' . $input['last_name'],
		'role'		=>	'None'

Notice above that I’m also using wp_generate_password to automatically generate the user’s password.

Here’s the thing: Although I’m creating the user, I’ve yet to actually add the zip code to the user’s profile.

To do that, we can take advantage of update_user_meta. Since the wp_insert_user function returns the ID of the user that has just been created, we can easily associate a zip code with the user:

update_user_meta( $user_id, 'zip_code', $input['zip_code'] );

Easy enough, but what good is this if we aren’t able to see it in a dashboard?

Viewing The Custom User Meta

To view the custom user meta that was just added, we need to do two things:

  1. Add a new column to the users dashboard
  2. Render the zip code that’s associated with the current user

WordPress provides two hooks for doing just this: manage_users_columns and manage_users_custom_column.

First, we’ll add the new column to the user dashboard (note the function comments, too):

  * Adds a Zip Code column to the user display dashboard.
  * @param	$columns	The array of columns that are displayed on the user dashboard
  * @return			The updated array of columns now including zip codes.
 function theme_add_user_zip_code_column( $columns ) {

	 $columns['zip_code'] = __( 'Zip Code', 'theme' );
	 return $columns;

 } // end theme_add_user_zip_code_column
 add_filter( 'manage_users_columns', 'theme_add_user_zip_code_column' );

Next, we’ll return the zip code associated with the user that’s currently being passed into the manage_users_custom_column action. We’re assigning this function a priority of 10 and noting that it accepts three arguments all of which are documented in the comments:

  * Populates the Zip Code column with the specified user's zip code.
  * @param	$value		An empty string
  * @param	$column_name	The name of the column to populate
  * @param	$user_id	The ID of the user for which we're working with
  * @return			The zip code associated with the user
 function theme_show_user_zip_code_data( $value, $column_name, $user_id ) {

	 if( 'zip_code' == $column_name ) {
		 return get_user_meta( $user_id, 'zip_code', true );
	 } // end if

 } // end theme_show_user_zip_code_data
 add_action( 'manage_users_custom_column', 'theme_show_user_zip_code_data', 10, 3 );

Note that we’re only going to return a value if the column name that’s being passed as an argument equals that of the custom column created above.

Assuming you’ve done everything correctly, you should see the following dashboard:

Add Custom User Meta During Registration in WordPress

Notice that if the user doesn’t have a zip code, then nothing is returned and the dashboard doesn’t bomb out with any ugly notices. If, on the other hand, the user does have a zip code, it’ll write it out in the column we created.

Of course, this is a simple example of what can be done with user registration, custom user meta, and adding fields to the user dashboard.