0

How can I add a field to the WordPress Register form? For example, I want to add the "First Name" field in the users profile to the Register form, but I can't figure out how to do that.

The field's name is first_name, but adding <input type="text" name="first_name" id="first_name" value="<?php echo $_POST['first_name']; ?>" required="required" placeholder="<?php _e('First Name'); ?>" /> doesn't work for some reason. The form submits fine, but the profile doesn't get the "First Name" field filled in in the back end.

I'm editing this plugin to tweak the register form: http://wordpress.org/extend/plugins/s8-custom-login-and-registration/

2 Answers 2

3

I don't know about the plugin you are using, but the way to add custom fields to the registration form and saving it to user profile is by hooking into register_form action hook to output the field and then using the appropriate filter/action hook to update/save user profile.

Your case, for example:

<?php
// output the form field
add_action('register_form', 'ad_register_fields');
function ad_register_fields() {
?>
    <p>
        <label for="firstname"><?php _e('First Name') ?><br />
        <input type="firstname" name="firstname" id="firstname" class="input" value="<?php echo esc_attr($_POST['firstname']); ?>" size="25" tabindex="20" />
        </label>
    </p>
<?php
}

// save new first name
add_filter('pre_user_first_name', 'ad_user_firstname');
function ad_user_firstname($firstname) {
    if (isset($_POST['firstname'])) {
        $firstname = $_POST['firstname'];
    }
    return $firstname;
}
?>
5
  • So this would add it to the default WordPress registration form, correct? Dec 13, 2012 at 21:24
  • Yes, that is correct.
    – Ahmad M
    Dec 14, 2012 at 8:37
  • 1
    @Ahmad M Is there any way to validation for these additional fields? Apr 6, 2013 at 5:08
  • Anyone knows how to add a field before username/email ? Everything is added after those two ...
    – trainoasis
    Aug 9, 2016 at 8:26
  • @Ahmad M's solution works perfectly today, but I try to add billing_phone (woocommerce) and I can't find a way to save this data in the profile... I would also like these fields to be mandatory, and if possible, remove the "username" "and generate it automatically with first and last name
    – Leo Danos
    Jan 25 at 19:53
0

I found an excellent, free plugin that gets the job done.

http://wordpress.org/extend/plugins/user-registration-aide/

Very easy to use, adds fields to both the register and the profile pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.