Skip to content

Instantly share code, notes, and snippets.

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
);
@hivepress
Copy link
Author

hivepress commented Sep 24, 2022

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

menoudyann commented Oct 20, 2022

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

@hivepress
Copy link
Author

hivepress commented Oct 21, 2022

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