Additional fields on registration (phone)

Hi there,
I am trying to add a phone number field on registration and have come quite far, but somehow when the form is submitted it does not save it in the database.

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,
				],
				
				'user_phone'  => [
					'type' => 'phone',				
					'required' => true,
					'_order'   => 3,
					'label' => 'Phone (WhatsApp)',
					'value' => '+34',
				],
			]
		);

		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['user_phone'] ) ) {
			update_user_meta( $user_id, 'user_phone', $values['user_phone'] );
		}
	},
	10,
	2
);


This is all done via the Custom Snippet tool.

First and Lastname work fine, but the phone number gets lost both on the front end and in the user settings on the admin page.

Also, after registration it does not redirect to settings as it usually does, but rather sends me to the main homepage.

Any idea what I am doing wrong?

Here is the attribute setup.

Hi,

Please try adding the “hp_” prefix to the user phone meta key when you save it, attributes in HivePress have this prefix in the database. Also, we plan to add this to the next update - attributes marked as both Editable and Required will automatically appear in the registration form.

Hope this helps

I have done that before, but here it goes again.
NOTE, I use the attributes on users, not vendors as this is for all users.

Here is the registration form:

These are the settings after registering as a user

I can edit the field here, but it does not take the input from the registration form.

And here is the update snippet

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,
				],
				
				'hp_phone'  => [
					'type' => 'phone',				
					'required' => true,
					'_order'   => 3,
					'label' => 'Phone (WhatsApp)',
					'value' => '+34',
				],
			]
		);

		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['hp_phone'] ) ) {
			update_user_meta( $user_id, 'hp_phone', $values['hp_phone'] );
		}
	},
	10,
	2
);

And here is the attributes setting, which should be fine

Please don’t add prefix to the field name in the attribute settings UI. If you set the “phone” field name there, and use the “hp_phone” meta key in the code it should be ok for sure, HivePress stores non-selectable field types (like Phone) as post meta fields with keys “hp_$field_name”.

1 Like

Thanks, now it works!