Adding First Name & Last Name to registration form (and adding to back end).
Hey folks,
New users to this, but have been busy as the last couple of days.
One thing I've noticed unfortunately in all my searching for help with stuff is that the support seems to be lacking. I've seen loads of thread from months / years back with questions that never get answered. Its frustrating. I work for a software company myself (in customer service - I'm not a coder) and I naturally expect the same level of support from others, that we give. Unfortunately not everyone else provides this level of support.
Anyhow, one question I noticed a lot, because it was something I was looking todo was adding some custom fields to the registration form.
The few posts I saw - including the paid plugin.... all added the fields to the bottom of the form, and things like captcha would be stuck in the middle if you tried moving things around. The response in the paid plugin forum was to 'pay more for more customisation'. Pffft.
So, I needed to add First Name & Last Name to my registration form.
I needed this information to be applied to the user profile as well in the back end (as I pull this information on the author page).
Lets begin:
------------------------
There are 2 files we need to edit; our functions.php file and the tpl-registration.php file.
In the tpl-registraion.php file we need to add a line of code which will serve as our start of our custom fields.
In our functions.php file is where we will be making the fields connect with the fields in the WordPress user profile
In tpl-registration.php you need to find the following line of code. It should be around line 45.
Code:
<form action="<?php echo appthemes_get_registration_url( 'login_post' ); ?>" method="post" class="loginform" name="registerform" id="registerform">
Directly underneath this we need to paste the following bit of code:
Code:
<?php do_action( 'register_form2' ); ?>
Next step is our functions.php file. We need to add the code that will render the form fields and submit them as part of the registration correctly. The best thing to do if you're not using a child theme is scroll to the very bottom of the file and paste the following code:
Code:
// output the custom fields
add_action('register_form2', 'ad_register_fields');
function ad_register_fields() {
?>
<p>
<label for="first_name"><?php _e('First Name') ?><br /></label>
<input tabindex="1" type="first_name" name="first_name" id="first_name" class="text required" value="<?php echo esc_attr($_POST['first_name']); ?>" size="25" tabindex="20" />
</p>
<p>
<label for="last_name"><?php _e('Last Name') ?><br /></label>
<input tabindex="2" type="last_name" name="last_name" id="last_name" class="text required" value="<?php echo esc_attr($_POST['last_name']); ?>" size="25" tabindex="20" />
</p>
<?php
}
// save new first name
add_filter('pre_user_first_name', 'ad_user_firstname');
function ad_user_firstname($first_name) {
if (isset($_POST['first_name'])) {
$first_name = $_POST['first_name'];
}
return $first_name;
}
// save new last name
add_filter('pre_user_last_name', 'ad_user_lastname');
function ad_user_lastname($last_name) {
if (isset($_POST['last_name'])) {
$last_name = $_POST['last_name'];
}
return $last_name;
}
I hope this helps - lots of people want to know how to do this.