Request the phone number in the subscrition form

Hello,

I would like to request the phone number in the subscription form (in addition to the first and last name) I tried to add this snippet code but that doesn’t work for the phone. How to do?

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,
				],
				
				'tel'  => [
					'required' => true,
					'_order'   => 5,
				],
			]
		);

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

Hi, tel is not the correct field name

Ok, thanks, what is the correct field name if I want required the phone number for a subscription? Maybe there is another kind of code?

Add an attributes in Vendors/Attributes section. Rename the field hp_telephone. Then change the field name to that on your code snippet. don’t use tel, I believe it is reserved & can’t be used.

Hope this helps.

1 Like

Hello, thanks for your answer, I tried your solution but that still doesn’t work, we use Rentalhive and I precise it could be useful for us and our customer service to have the phone number of all the users who subscribe (not only host), so I tried with user attribute too but that doesn’t work. Maybe the snippet code is not correct?

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_telephone'  => [
					'required' => true,
					'_order'   => 5,
				],
			]
		);

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

Thank you for waiting. Please try this PHP code 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_telephone'  => [
					'type' => 'phone',
					'country' => 'FR',
					'required' => true,
					'_order'   => 5,
				],
			]
		);

		return $form;
	},
	1000
);

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_telephone'] ) ) {
			update_user_meta( $user_id, 'hp_hp_telephone', $values['hp_telephone'] );
		}
	},
	1000,
	2
);
1 Like

Thanks, that’s work just one last question about this topic, should I add a css code if I want to display “telephone” text in the form?

Capture d’écran_2023-10-04_21-36-19-b

Please try this PHP code snippet to add the title to the phone field.

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_telephone'  => [
					'label' => 'Telephone',
					'type' => 'phone',
					'country' => 'FR',
					'required' => true,
					'_order'   => 5,
				],
			]
		);

		return $form;
	},
	1000
);

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_telephone'] ) ) {
			update_user_meta( $user_id, 'hp_hp_telephone', $values['hp_telephone'] );
		}
	},
	1000,
	2
);
2 Likes

Ok, that’s work, thanks a lot :+1:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.