Add Custom Field to Register Form

this is gonna be a quick one, cause I need to finish website. But I got this working today. This is for function.php file or in a plugin will work. This will add two forms to the register page in your wordpress, First Name and Last Name.

add_action(‘register_form’,'show_first_name_field’);
add_action(‘register_post’,'check_fields’,10,3);
add_action(‘user_register’, ‘register_extra_fields’);

function show_first_name_field(){
?>
<p>
<label>First Name<br/>
<input id=”user_email” type=”text” tabindex=”20″ size=”25″ value=”<?php echo $_POST['first']; ?>” name=”first”/>
</label>
</p>
<p>
<label>Last Name<br/>
<input id=”user_email” type=”text” tabindex=”20″ size=”25″ value=”<?php echo $_POST['last']; ?>” name=”last”/>
</label>
</p>
<?php
}

function check_fields($login, $email, $errors) {
global $firstname, $lastname;
if ($_POST['first'] == ”) {
$errors->add(‘empty_realname’, “<strong>ERROR</strong>: Please Enter in First Name”);
} else {
$firstname = $_POST['first'];
}
if ($_POST['last'] == ”) {
$errors->add(‘empty_realname’, “<strong>ERROR</strong>: Please Enter in Last Name”);
} else {
$firstname = $_POST['last'];
}
}
function register_extra_fields($user_id, $password=”", $meta=array())  {

$userdata = array();
$userdata['ID'] = $user_id;
$userdata['first_name'] = $_POST['first'];
$userdata['last_name'] = $_POST['last'];
wp_update_user($userdata);
}

About Weston Deboer

I like to use wordpress for any and every website that I work on. No matter how big or how small the website is.
This entry was posted in Wordpress. Bookmark the permalink.

24 Responses to Add Custom Field to Register Form

  1. kunal says:

    Thanks for the info. Is it possible to add fields that do not exist in the profile page of wordpress (eg. Company Name)?

    Also, do you know if this will work with the register plus plugin? I am gradually trying to get rid of it as it has not been updated for a while but I need it to let users set their own password.

  2. Of course kunal!

    I don’t have any experience with the register plug plugin. But I have allowed users to create there own password on the register screen also. It is not quite perfected at the moment, but I will keep you updated.

    Now back to adding fields, if this is to complicated i am sorry and also I am just looking at the code and not using real world testing here, but it should work:

    After:

    wp_update_user($userdata);

    Put:

    update_usermeta($user_id, ‘company’,$company);

    I am using a custom register form for all of this and not using wp’s internals to do it.
    example: http://smartsavingsusa.com/register/

  3. kunal says:

    Thanks for the help! Ill try this out.

    The idea of using a custom register form is great (Defult WP registration does not really fit well as my site is not a blog). Does this integrate with WP’s user modules? Will you be posting a tutorial on how to set up a custom registration form?

    I’m trying to set up a website which needs registrations for regular users as well as sponsors. The best way to accommodate this would be if I could have 2 different registration forms that set different default roles for users.

  4. ralcus says:

    Hi, Weston Deboer thanks for this it’s a great help. My question is to do with tab order. Currently using your code above the new fields are added but the tab order between the original fields and the new ones is on of sync.

    I notice on your example: http://smartsavingsusa.com/register/ is perfect.

    Is this because you removed the default fields and replaced them all with your own?

    great articles, glad i stumbled across your blog.

  5. ralcus says:

    Sorry forget those comments, i was being too hasty i didnt notice tabindex in the html, i removed them and its fine. I assumed the hook loaded the extra fields at a strange place so that messed the tab order but all i had to do was pay more attention to the markup, sorry.

  6. Enrique Acevedo says:

    Muy buen ejemplo me has ayudado mucho, recomiendame un libro donde explique las funciones del wordpress.

  7. I didn’t learn this from a book. I wish i could recommend something!

  8. Pingback: Adding Custom Fields to User Registration in Wordpress « X2 Consulting Blog

  9. John Wallace says:

    Thanks for your code. With some clues from Justin Tadlock and Yoast I came up with a solution that works for me. To add custom fields to the registration form, first add them to your user profile. Try this:

    function add_custom_contactmethod( $contactmethods ) {
    // Add Contact Fields
    $contactmethods['twitter'] = ‘Twitter’;
    $contactmethods['address'] = ‘Address’;
    $contactmethods['city'] = ‘City’;
    $contactmethods['state'] = ‘State’;
    $contactmethods['postalcode'] = ‘Postal Code’;

    // Remove Unused Contact Fields
    unset($contactmethods['yim']);

    return $contactmethods;
    }
    add_filter(‘user_contactmethods’,'add_custom_contactmethod’,10,1);

    add_action(‘register_form’,'show_first_name_field’);
    add_action(‘register_post’,'check_fields’,10,3);
    add_action(‘user_register’, ‘register_extra_fields’);

    function show_first_name_field(){ ?>

    First Name
    <input id="user_email" type="text" tabindex="20" size="25" value="” name=”first”/>

    Last Name
    <input id="user_email" type="text" tabindex="20" size="25" value="” name=”last”/>

    Address
    <input id="user_email" type="text" tabindex="20" size="25" value="” name=”address”/>

    City
    <input id="user_email" type="text" tabindex="20" size="25" value="” name=”city”/>

    State
    <input id="user_email" type="text" tabindex="20" size="25" value="” name=”state”/>

    Postal Code
    <input id="user_email" type="text" tabindex="20" size="25" value="” name=”postalcode”/>

    add(‘empty_realname’, “Please Enter in First Name”);
    } else {
    $firstname = $_POST['first'];
    }
    if ($_POST['last'] == ”) {
    $errors->add(‘empty_realname’, “ERROR: Please Enter in Last Name”);
    } else {
    $firstname = $_POST['last'];
    }
    }
    function register_extra_fields($user_id, $password=”", $meta=array()) {

    $userdata = array();
    $userdata['ID'] = $user_id;
    $userdata['first_name'] = $_POST['first'];
    $userdata['last_name'] = $_POST['last'];
    wp_update_user($userdata);
    update_usermeta( $user_id, ‘address’, $_POST['address'] );
    update_usermeta( $user_id, ‘city’, $_POST['city'] );
    update_usermeta( $user_id, ‘state’, $_POST['state'] );
    update_usermeta( $user_id, ‘postalcode’, $_POST['postalcode'] );
    }

  10. abdullah.okm says:

    hay,

    thanks for the tips,but how can I update the same customfields into database.
    Pls help me?

  11. jonty says:

    hello i have a problem i need two registration form for my site having different code i am new in wordpress so pls help me out thanks a ton

  12. alex says:

    Hey john,
    Im having trouble with the code you posted..
    there is something missing i think. cud you check it out..
    pls cud you be specific as to which file i shud copy this code too. im new to WP.
    Thank .

    Hi weston,
    I tried your code too and im getting the filed displayed but i can seem to arrange them in order.. the fields are coming at the bottom.
    Thanks.

  13. alex says:

    Ralcus,
    The registration page you gave as link is perfect..
    can you post me the code and where i shud copy them..
    Thanks.
    Im glad i stumbled on this post.
    I hope to find wht i came for.
    Thanks all..

  14. Earthman Web says:

    Has anyone discovered a plugin or method for generating custom registration screens for signing people up for different roles?

    The main goal I am working towards is to include different custom fields for each specific user role…

  15. Jason says:

    Is there a way to assign a role to the user using this method?

  16. Yes, you would just use update_user_meta http://codex.wordpress.org/Function_Reference/update_user_meta
    so at the bottom of the code where it says wp_update_user($userdata);
    you would just put in:
    update_user_meta($user_id, ‘user_level’, ’1′);

    and of course the 1 represents the user level that you want them to have.

  17. alex says:

    Hi, I had tried the code above but then I came across this plugin called register plus which served my purpose without much coding and it could help here to. It is working fine on my 2.92 WP.
    has a lot of features…

  18. Prosenjeet says:

    Alex,
    Register plus no longer works perfect in version 3.01, bugs keep cropping up. I faced the captcha issue…that is while registering, feeding correct captcha gives “wrong captcha” error.
    Any new plugin? Anyone….

  19. Keven Marin says:

    Have you been able to do the same thing for WordPress 3.0.1 with Multisites activated?

    I’ve been searching for a day an I can’t find a way to update my custom fields.

    I have all my meta in wp_signups (meta) but when I try to update the wp_usermeta table at the activation (wpmu_active_user), no data are being updated.

    I have used these functions as hook for my WPMS 3.0 custom register form :
    - signup_extra_fields() // Add extra field to the form
    - wpmu_validate_user_signup() // Add validation to extra fields
    - add_signup_meta() // Add meta to wp_signup waiting for confirmation
    - wpmu_activate_user() // add update for extra field to user’s registration.

    In wpmu_activate_user() callback function I’ve tried to update my user meta with these functions without success:
    - update_user_meta()
    - update_usermeta()
    - wp_update_user()

  20. Tamojyoti Bose says:

    Your post and comments save me at the 11th hour of the delivery. Thank u very much

  21. By the way I especially like on this site: http://smartsavingsusa.com/register/
    how you allow the user to create their own password and then upon registration they are automatically logged into website without having to go check email and get crazy generated password

    Maybe you have any hints on that? Much appreciated; else, I keep working to figure this out just myself

    Thanks again, nice day

  22. Mark Casey says:

    I was having a bit of trouble with this code. Copying and pasting from this page was picking up some rouge characters like ” etc.

    Anyway, here’s a cleaned up version which works fine for me.

    http://code-bin.homedns.org/929

  23. BrenFM says:

    Thanks heaps for the code here! VERY heplful for custom registration functionality! And thanks particularly to John Wallace!

  24. Jessica says:

    I think there’s a plugin already which done more than it and you can create many fields and extend the user profile fields in WordPress, Yes and also it’s allow admin to charge fee for signup (optional) as well as user can set their own password, Custom Notification Emails and much more via Pie-Register

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>