Add a file attachment field in the user registration form

I want to add a file attachment field (Required) in the user registration form of the listinghive theme. I have added a custom code using a code snippet in the child theme. I am getting the error that the 'id is required even if I have already uploaded the file.

please help me with this as soon as possible

i have added this code to the 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,
				],
				
				'id'  => [
					'type'=> 'file',
					'format'=>['jpg', 'png', 'gif'],
					'required' => true,
					'_order'   => 123,
				],
			]
		);

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

thanks and regards
Shweta Ruparelia

Unfortunately there’s no simple snippet for this in the current version, attachments must be assigned to some user ID (the user ID field is required for the Attachment model) but it’s not available on registration. A possible solution is to upload the file temporarily somewhere, and using it in the user register action, saving it for a newly created user profile.

I recommend using a custom Attachment attribute for vendors if this attachment is meant for vendors, they will not be able to save profile form or add a listing without uploading the attachment in this case, and code changes will not be required.

1 Like

thanks for your valuable guidance, as per your recommendations I have created a custom attachment attribute for vendors.

but I am still having this error

can you please guide me to where I have made a mistake in creating this?

I am sharing the backend attribute screenshot and front-end error screenshot here,

Hi,

Please try adding vendor-specific fields with attachment type in Vendors/Attributes (if you mark them as Editable and Required, then users who attempt to list something will have to fill them in for their profile).

1 Like

i have created as you said,

but i am still having the error id upload is required if i have already uploaded it

Hi,

We checked this issue from our side, and it seems okay.

Please disable third-party plugins and customizations (if there are any) and check if this issue persists. If you use a caching plugin, ensure that caching is disabled for logged-in users.

Also, please check in more detail the CSS or PHP snippets you have added; there may be a conflict with them.

If this issue persists, please send temporary WP access to support@hivepress.io with details for reproducing this issue, and we’ll check it. You can create a temporary access link using this plugin Temporary Login Without Password – WordPress plugin | WordPress.org.

Hi, try changing the field name to something else. Some words conflict with the programming if it’s already being used elsewhere.

2 Likes

hii… thanks a lot for making my day, :slightly_smiling_face: I got the solution by changing the field name.

have a good day

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