Add fields to registration form

Hello. I am trying to add first name, last name, company, and location to the registration form and member profile area but can’t get it to work correctly. I tried modifying what I found on another post but need help please.

add_filter(
	'hivepress/v1/forms/user_register',
	function ( $form ) {
		$form['fields'] = array_merge(
			$form['fields'],
			[
				'first_name' => [
					'required' => true,
					'_order'   => 1,
				],

				'last_name'  => [
					'required' => true,
					'_order'   => 2,
				],
				
				'organization/company'  => [
					'required' => false,
					'_order'   => 3,
				],
				
				'location'  => [
					'required' => false,
					'_order'   => 4,
				],
			]
		);

		return $form;
	},
	100
);

add_action(
	'hivepress/v1/models/user/register',
	function( $user_id, $values ) {
		if ( isset( $values['first_name'] ) ) {
			update_user_meta( $user_id, 'first_name', $values['first_name'] );
		}

		if ( isset( $values['last_name'] ) ) {
			update_user_meta( $user_id, 'last_name', $values['last_name'] );
		}
		
		if ( isset( $values['organization'] ) ) {
			update_user_meta( $user_id, 'organization', $values['organization'] );
		}
		
		if ( isset( $values['location'] ) ) {
			update_user_meta( $user_id, 'location', $values['location'] );
		}
	},
	10,
	2
);

Please try the code snippet from this topic First Name not asked while registering. | HivePress Support. There is a first name and last name fields. In the same way, it is possible to add other fields

Also, it is important to add latitude and longitude fields with location field in this way

'location' => [
	'label' => 'Location',
	'type'    => 'location',
	'_order' => 1,
],

'latitude' => [
	'type'    => 'latitude',
	'_order' => 1,
],

'latitude' => [
	'type'    => 'longitude',
	'_order' => 1,
],

Please note that the location field can cause problems for regular users but it is needed testing

1 Like