Here’s yet another useful PHP snippet – and a mini-plugin alternative with super simple settings – that adds the Billing First Name and Billing Last Name to the Registration Form on the WooCommerce My Account page.
By default, the WooCommerce customer Registration Form only displays the email, username, and password fields (unless username and password are set to “automatically generate“, in which case only the email input field will be visible); by adding more billing and/or shipping fields to the form we can of course capture more information about each customer, so that you can do your background checks or allow them to save time at their next purchase.
Either way, enjoy!
1) PHP Snippet: Add First & Last Name to WooCommerce My Account Registration Form
/**
* @snippet Add First & Last Name to My Account Register Form - WooCommerce
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 8
* @community https://businessbloomer.com/club/
*/
///////////////////////////////
// 1. ADD FIELDS
add_action( 'woocommerce_register_form_start', 'bbloomer_add_name_woo_account_registration' );
function bbloomer_add_name_woo_account_registration() {
?>
<p class="form-row form-row-first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="form-row form-row-last">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
///////////////////////////////
// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'bbloomer_validate_name_fields', 10, 3 );
function bbloomer_validate_name_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( '<strong>Error</strong>: First name is required!', 'woocommerce' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( '<strong>Error</strong>: Last name is required!.', 'woocommerce' ) );
}
return $errors;
}
///////////////////////////////
// 3. SAVE FIELDS
add_action( 'woocommerce_created_customer', 'bbloomer_save_name_fields' );
function bbloomer_save_name_fields( $customer_id ) {
if ( isset( $_POST['billing_first_name'] ) ) {
update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) );
}
if ( isset( $_POST['billing_last_name'] ) ) {
update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
update_user_meta( $customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']) );
}
}
2) Mini-Plugin: Business Bloomer WooCommerce Add Customer Fields To My Account Registration Form
You don’t feel confident with coding? You don’t want to purchase yet another bloated, expensive plugin? Great!
Business Bloomer WooCommerce Add Customer Fields To My Account Registration Form Mini-Plugin comes without the usual WordPress plugin hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WordPress notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. Super simple settings.
Speaking of which, here’s a screenshot of the settings:
Quick demo:
As you can see the plugin is straight forward. Install it, pick which fields you want to add to the My Account registration form, and save. Simple!
I have a custom post to be created when a new user registers, I tested this snippet, and it works, but how can I fire the form update from Account Details? I want to update the custom post’s title with the new updated customer’s first and last name.
Hi Kelvin, you can use the hook “woocommerce_customer_save_address”:
How can I add my custom field between two default Woocommerce fields?
Kelvin
Not be possible with PHP alone – CSS or possibly JS may be needed to move them before/after the field you like
Yes, i have added it and it works just fine.. Thanks for the contribution
Awesome
This works brilliantly, thank you. Does anyone know how to add alphabet validation (disallow special characters) to this code? Then it would be perfect! Thanks again for sharing.
Hi Brad, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Awesome shortcode! Works great – just have a issue with one thing:
Is it possible to redirect the user if logged in to the my account page? If yes, how shall I do it?
Hi Jonathan, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo,
This is the snippet I was looking for. It works great.
But I was wondering how can I combine it with the Separate WooCommerce Customer Registration shortcode?(https://www.businessbloomer.com/woocommerce-separate-login-registration/) I’ve been having problem in combining the 2 together.
Hello Earth, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
This is exactly what I wanted for the front end, but now I can not add users via the dashboard…I am getting the following error:
Cannot create a user with an empty login name.
How do I still add people via the Dashbaord?
That’s weird. Sure if you remove the customization this goes back to normal?
Hello Rodolfo,
Thx for your solution, I added the name, last name, pass , confirm pass.
And now I have a structure like this: name, last name, pass , confirm pass, email.
How can I do to be like this: name, last name, email, pass , confirm pass ?
Thx!
Hi Igor, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo
I used this on Kadence Ascend child theme – worked first time.
It would be useful if Woocommerce would address the new account registration email(and others) to the users first/Last name rather then the username which would add a touch of professionalism – don’t you think?
WP 5.5.1 running Kadence Ascend Premium with Ascend Child Theme.
I agree
Tried this with rehub theme but it didnt make any changes to the login popup.
Stylianos, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Just tried it out on WC 4.1 and still works like a charm! Thanks for the snippet
Great!
I’ve tried to override the login_form from template (copied from plugins to themes) through cPanel but still does not work. Does my wp version 4.1.0 matters regarding this specific snippet?
Thanks again.
I don’t recommend to copy templates, sorry
Hey Rodolfo,
Thanks for the free info you provide to us.
I am not a developer, I paste this to the correct file using a child theme. All i get is this,
”Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.”
Do i need to change something on the code? Maybe replace some fields with my site name?
Thanks again.
Don’t use the WordPress editor – use FTP instead
Thanks Man, Work like a charm
Cool!
Hi,
Is it possible to set maximum field length for the first and last name?
Hi Markos, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Your code seems to work well except when I found that there is no First and Last name recorded in my users data. Please suggest.
Hi Nara, did you change anything in my snippet?
No. I copied them all. I noticed that you have updated both first_name and billing_first_name with the name acquired but my finding is that only billing_first_name is updated.
Uhm, honestly it should work. I just tested it again and it worked indeed
Update. I found an error in wordpress log as following:
https://prnt.sc/rkwram
and line 51 is
https://prnt.sc/rkwrw4
It’s exactly as you code. What do you think ?
No idea – snippet is fine I’m afraid
Hello thank you for the code, just a question if I want to add another data for example the ID and show on the page my account. How could I do that?
Hi Jose, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Excellent code! The script worked well, except for the validation. The fields validate, but does not return errors.
Uhm, it should work. Try with no plugins but Woo, and 2020 theme – does it work?
Hello, great code, thanks a lot. If I’d like to use those fields in the confirmation email to the customer as the salutation, instead of the user name that’s being used right now… I thought about just replacing with billing_first_name but sadly enough that’s not working.
Can you help this noobie a bit with the correct line?
Hi Elkin, did you mean this: https://businessbloomer.com/woocommerce-add-billing-first_name-to-email-receipt/
Is this working with Dokan? I have problem with that
Not sure, ask Dokan
Hello Rodolfo,
Thanks for all the codes you share! They are making my website coming along. After adding name and surname, is it possible to create the username automatically? Like the first letter of name and the surname.
Hello Firat, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Working on my site and thanks for the great tutorials!
I have a feeling this is going to start a long road of learning WordPress customizing for myself!
Most recent version of WordPress (as of 09-25-2019) using a third party parent theme, Soledad.
Excellent!
Worked like a charm! Great job Rodolfo! Thank you. Thoughts on making this its own file and calling to it from the functions file? Only because eventually our functions file is going to be a mess of hooks and snippets. Would love to know from a security stand point too.
Thanks again
Cool! If you really want, you can turn each snippet into a standalone plugin: https://codex.wordpress.org/Writing_a_Plugin#Creating_a_Plugin
Hi Rodolfo,
I just used your code and it works wonderfully. However, first and last name are on separate line, where with your example, it is on the same line. What could be the reason.
Maybe just some CSS that needs to be customized 🙂
Thank you for this code. It is working on the latest wordpress and woocommerce versions
Awesome!
Ciao e complimenti, ho inserito il codice e tutto funziona bene.
Vorrei chiederti se fosse possibile, inserire anche il campo Telefono e se puoi darmi indicazioni in tal senso.
Ti ringrazio anticipatamente e attendo un tuo gentile riscontro.
Un saluto.
Emi
Ciao Emiliano, yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Beautiful idea. I hate when the name is blank. Or when it is set to a part of the email address.
I found that it’s good to also save the name into the user’s display_name too.
wp_update_user (array( ‘ID’ => $customer_id, ‘display_name’ => $_POST[‘billing_first_name’] . ‘ ‘ . $_POST[‘billing_last_name’] ) );
Thank you!
Please help
In my country it all the way around Last Name come first then “First name”. So can you guys help me with the code to
1. Reoder first name and last name field all the way around.
2. Put Last name and first name as the Display name automatically.
Thank you so much for reading.
Hi,
This is a great overview/snippet for woocommerce my account page. After reviewing the code, I wanted to ask if there isa way to add address information to the My Account form and also to the wcpv_registration form? if so, could you please explain?
Thank you in advance.
Hello John, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Thanks, Rodolfo for the code,
Is there any way to make the last name optional in this?
Hey Keerthan, thanks for your comment! Yes, sure, just remove the validation for that field and also the “required” span 🙂
Hi Rodolfo, I am using this snippet and its working just great. However, I am also using another snippet of yours to add text to the registration screen (https://businessbloomer.com/woocommerce-show-extra-content-my-account-page/) but the first name and last name fields show above the extra text as opposed to below it.
Any help would be very much appreciated.
Hey Brian, thanks for your comment 🙂 Just add the priority e.g. “15”:
I added the code provided by Luuk and the names still don’t stick in the user interface unless the customer inputs their name again. Any additional suggestions would be very appreciated.
Robin, thanks a mill for your comment. Not sure about Luuk’s code, unfortunately I cannot troubleshoot it for you here via the blog comments. Thanks for your understanding, R
Hey Rodolfo, It works! Thanks for the code. No plugin required and very easy to implement (if you know little bit of coding). Also name and surname is visible in WordPress database.
Nice 🙂
but getting error when debug is true
Undefined variable: options in C:\Program Files (x86)\Ampps\www\wp\wp-content\themes\themename\includes\woocommerce.php on line 270
Hey there, thanks for your comment! I don’t think my snippet is causing that – either your theme needs updating or there is another PHP conflict. Try with another theme and let me know if the snippet works 🙂
Its working fine, but if we enable debug is true then showing error, using php 7.1
Ok thank you! What PHP error do you get?
Notice: Undefined variable: options in C:\Program Files (x86)\Ampps\www\wp\wp-content\themes\xxx\includes\woocommerce.php on line 312
I don’t see any $options variable in my code, so it must be something else 🙂
Thanks so much for the great snippet Rodolfo & Luuk! So helpfull! 🙂
I am not a developer AT ALL so please forgive my lack in dev lingo and code structure. 🙂
I was looking for a way to also add the “company name” to the registration form. (billing_company according to https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ )
So first name, last name & Company name in top of the registration form.
And also display this saved data when logged in as a customer and viewing my billing- and account info.
I have tested the snipped and also added the company field & LUUK´s two extra lines to save first- and lastname. Code below.
Ive tested it on a demopage with WC version 3.2.6 and WP-version 4.9.2 and it seems to be working….but again… Im no developer, so I probably missed something.
Grateful for feedback. 🙂
Thanks again all of you!
Hello Mike,
Did you find the way that how to sync these two fields of First Name & Last Name on Billing Details & Account Details? Or is there anyway, if we hide the fields on Account Details tab and whatever information will be stored in the billing details the same goes to my account fields? Thanks
Sorry couldn’t get it to work. In the admin under users first and last name are still blank. Fields only show on front end but the data input is not collected and stored.
Hey Ryan, thanks for your comment! I just tested again and it works – please note “First” and “Last name” are the ones in the Customer Billing section (scroll down in the user page), and not the standard WordPress fields. Let me know 🙂
Hi I have seen that there are other person with this problem. The first and last name is not show in the user page (in any fields). Maybe it’s about the language? What test can I do? I have +100 users and I don’t have any first and last name
Thanks
Already tried with 2020 theme and no other plugins but Woo?
Hi there.
I’ve tried to use the same code and the fields are getting displayed. However, the first name and last name are not saving and not displaying for customer until he/she filled the same again in /my-account/edit-account/ section. What could be the cause for this? WooCommerce version 3.10.
Hey Ravi, thanks for your comment! Did you change anything in the snippet?
Same problem here… i do not make changes to the snippet. Can you help please?
You need to change the variables in template form-edit-account.php from “first_name” to “billing_first_name”
Same problem here. @tom are you saying the name fields in the account edit page are for the WordPress profile name and not the woocommerce billing firstname and lastname? Cheers!
Ah yes, Tom is correct.. So it is saving the name but you dont see it on the edit account page, only in the cart. I remember this issue from a while back but I’m fairly new to woocom again and assumed this wouldn’t still be an issue years and years later.. so there are two first name and last name fields per user.. what a strange way to do it.. anyways. I just need to figure out how to sync those fields now.
🙂
@Tom if you do this “form-edit-account.php from “first_name” to “billing_first_name””, the first time you go to /my-account/edit-account/ page is ok but if you modify first_name or last_name, saves changes and refresh this page, you won’t see your modification (because this form modifies firt_name and last_name not billing_first_name and billing_last_name which are the fields what this form use to auto prefill) Can anybody help us to fix this???
Hi all,
Rodolfo, thank you for this snippet!
To have the entered first and last names also show up in the My Account page, I added two lines in the saving section. Now the names are saved as billing names as well as ‘normal’ (WordPress) names. Below my edited version of the function used at //3. SAVE FIELDS. 🙂
Excellent 🙂