user_registeraction-hookWP 1.5.0

Fires immediately after a new user has been registered on the site and the user data has been added to the database.

The hook receives the user ID as a parameter.

When this hook fires, all metadata has already been added to the database, and the password has already been hashed.

This hook can be used to add additional metadata submitted through the new user registration form.

The insert_user_meta hook can also be used to add or update user metadata. See the example below or the wp_insert_user() code.

Usage

add_action( 'user_register', 'wp_kama_user_register_action', 10, 2 );

/**
 * Function for `user_register` action-hook.
 * 
 * @param int   $user_id  User ID.
 * @param array $userdata The raw array of data passed to wp_insert_user().
 *
 * @return void
 */
function wp_kama_user_register_action( $user_id, $userdata ){

	// action...
}
$user_id(int)
ID of the registered user.
$userdata(array)
Raw data array passed to wp_insert_user().

Examples

#1 Add additional user data during registration

This example shows how to add the user_sex field value submitted in $_POST data from the registration form.

Keep in mind that the data must not be validated when this hook fires: it is already too late because the user has already been added. Validate the data on the registration_errors hook. The user_register hook will not fire if validation fails.

// Validate the field in advance.
add_filter( 'registration_errors', 'my_validate_user_data' );
function my_validate_user_data( $errors ){
	if( empty($_POST['user_sex']) )
		$errors->add('empty_user_sex', 'Sex is required!' );
	elseif( ! in_array($_POST['user_sex'], array('male','female')) )
		$errors->add('invalid_user_sex', 'The specified sex is invalid!' );

	return $errors;
}

// Update user metadata.
add_action( 'user_register', 'my_user_registration' );
function my_user_registration( $user_id ) {
	// $_POST['user_sex'] was validated in advance.
	update_user_meta( $user_id, 'user_sex', $_POST['user_sex']);
}

#2 Update user metadata during registration

This is similar to the first example but uses the insert_user_meta hook to add user metadata during registration. This approach is preferable because it is more convenient.

This example completely replaces the user_register hook from the previous example. Use the validation code from that example.

// $meta = apply_filters( 'insert_user_meta', $meta, $user, $update );
add_filter( 'insert_user_meta', 'my_user_registration_meta', 10, 3 );
function my_user_registration_meta( $meta, $user, $update ) {
	// Return if this is not user registration.
	if( $update ) return $meta;

	$meta['user_sex'] = $_POST['user_sex']; // $_POST['user_sex'] was validated in advance.

	return $meta;
}

Changelog

Since 1.5.0 Introduced.
Since 5.8.0 The $userdata parameter was added.

Where the hook is called

wp_insert_user()
user_register
wp-includes/user.php 2711
do_action( 'user_register', $user_id, $userdata );

Where the hook is used in WordPress

wp-admin/includes/admin-filters.php 111
add_action( 'user_register', array( 'WP_Internal_Pointers', 'dismiss_pointers_for_new_users' ) );
wp-includes/default-filters.php 121
add_action( $action, 'wp_maybe_update_user_counts', 10, 0 );