Adding a new registration field select type

Hi. Since I couldn’t find an answer on the forum, I ask for your help.

  1. How can I add a field like select for vendors to the registration form? I tried to do it with the following code:
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_acctype'  => [
					'type' => 'select',				
					'required' => true,
					'_order'   => 3,
					'label' => 'Account type',
					'options'  => [
						'worker'   => 'Worker',
						'company'   => 'Company',
					],
				],
				
			]
		);

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

The field of select type appeared, but after successful registration and going to the profile this field is set by default. Why? Can you tell me where I made a mistake?
Vendor attribute (image)


  1. And how can I get information about which option was selected, Worker or Company, so that in the future for example using js to show different text/images for different types of accounts?
    Thanks

Hi,

Please try to create user attributes in WP Dashboard > User > Attributes and mark it as Editable and Required, and then it will be displayed during registration. After that, you need to synchronize this value with the vendor profile (using custom code), because there is no way to add the vendor attribute to the registration. Also, in future updates, we plan to add a feature to synchronize vendor and user attributes.

This is partially the solution to my problem. Thank you. Second question that I didn’t get an answer to. How can I get from the database the value of the select field that a certain user has selected?


As a result, in the process of searching on my own, I found this data in the database, table wp_usermeta. ID meta_value of this field can be found in table wp_terms. Thanks anyway

1 Like

Sorry for the delay.

Yes, selectable attributes are stored as taxonomy terms in wp_terms table (or in case of user attributes, as meta fields in wp_usermeta). You can also access any values in the PHP code if you have the HivePress user object, for example:

$user->get_accounttype();

1 Like

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