Skip to content

Instantly share code, notes, and snippets.

@hivepress
Created April 26, 2022 16:58
Show Gist options
  • Save hivepress/232ba63de5115a56dc21cd7f64c6e52f to your computer and use it in GitHub Desktop.
Save hivepress/232ba63de5115a56dc21cd7f64c6e52f to your computer and use it in GitHub Desktop.
Add first and last name fields to the user registration form #hivepress #users
<?php
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,
],
]
);
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'] );
}
},
10,
2
);
@menoudyann
Copy link

is it possible to add other fields like birth date or phone number with this snippet?

@hivepress
Copy link
Author

It's possible, but further customizations would be required to validate, save and display the field values somewhere. It's possible without customizations already, but for vendors only (by adding attributes in Vendors/Attributes section).

@shwetakotecharuparelia
Copy link

help me to add a custom field of attaching a file as an id card (required) at the time of registration. I have applied the same method of the code given above. it works with first name and last name, but it shows an error 'id is required' even if the id file is uploaded.
registration form file required error

i have applied this code in the child theme using code snippet

registration form file required error code

@shwetakotecharuparelia
Copy link

I want to add a file attachment field in the registration form, I want the user to upload his/her identity card which is mandatory at the time of registration I have applied the same method of the code given above. it works with first name and last name, but it shows an error 'id is required' even if the id file is uploaded.

registration form file required error
registration form file required error code

please help me with this

@hivepress
Copy link
Author

Please try using our community forum for requests like this one, if there's no simple code snippet we can provide some guidance if you're familiar with WordPress development https://community.hivepress.io/

@onurb-san
Copy link

Has this been resolved?

@hivepress
Copy link
Author

If you mean adding first/last name to the registration fields, please try using the code snippet above. If you mean the attachment field in the registration form, this requires extra customizations at the moment.

@onurb-san
Copy link

It would be to send an attachment during registration. Could you provide a code for this?

@hivepress
Copy link
Author

Unfortunately there's no simple temporary fix, this has to be changed in the framework. If you require an attachment from vendors (those users who want to add listings), you can require an attachment via the vendor registration form

@cperrine76
Copy link

Hello, I know it has been a while but I was hoping someone may have an answer. I tried the code and indeed I eventually added the first and last name to the registration box. After I realized that wasn't what I needed, I tried to deativate and delete the code snippet (wp Code Lite) they now won't go away? I have tried so many caching fixes that do not work. This is a subdomain of my main site fyi. Even with incognito mode it won't revert. It is fine on my mobile but not laptop on multiple browsers.

@hivepress
Copy link
Author

Hi,

Please make sure that you removed the snippet. If you used the Code Snippets plugin and it wasn't added to any of the theme/plugin files directly, then clicking Delete on the snippet should be enough. Another reason may be caching, e.g. if the hosting provider caches the website, or there's a caching plugin in WordPress/Plugins.

@cperrine76
Copy link

cperrine76 commented Mar 12, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment