Skip to content

Instantly share code, notes, and snippets.

@hivepress
Created April 26, 2022 16:58
Embed
What would you like to do?
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
);
@mishoothedrumkit
Copy link

Thank you! Very helpful!

@M-Alfaris
Copy link

I added this code, but it shows me only one field for username! I want to add another one for first and last name. how can I fix this?

@hivepress
Copy link
Author

Please make sure that you added it correctly (e.g. via the Code Snippets plugin), when I test it locally it adds both fields https://prnt.sc/MaxfMAkQFeVg

@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).

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