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

mishoothedrumkit commented Jun 11, 2022

Thank you! Very helpful!

@M-Alfaris
Copy link

M-Alfaris commented Sep 23, 2022

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

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

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