Showing Additional Contact Fields in WordPress Registration Form
In this simple, three step tutorial you’ll see how easy it is to modify list of contact method fields being used in WordPress user profiles and display those fields in WordPress registration form. Tools needed to achieve this are custom fields from usermeta table, one WordPress filter hook and two WordPress action hooks.
This could be very useful for WordPress powered websites that allow anyone to register and display user (author) information attached to their posts, on authors’ profile pages or anywhere else.
OK, so let’s start by getting rid of the old and welcoming the new. Raise your hand if you used Yahoo IM, AIM or Jabber (seriously WordPress, no ICQ??) fields in WordPress user profiles. Good, you’re the 1%, shame on you. In order to drop those three and add Twitter, Facebook and Google+, we need to hook our custom function into a WordPress filter hook, or in plain English – modify output of a WordPress function.
Our custom function, ts_modify_contact_fields() intercepts $contactmethods array before it’s being output, removes the AIM, YIM and Jabber and adds Twitter, Facebook and Google+ instead. You can add this code to your functions.php and check out your user profile from the dashboard. If you’re not already familiar with add_filter function, you should definitely check out its Codex page.
Now that we have stepped into the 21st century and social networks have replaced AIM, YIM and Jabber we can show off this new data on our website by using get_user_meta() function. For example, in order to show official Twitter Follow button you should do this:
What that block of code does is check if user with ID = $user_id has entered Twitter info, and if the answer is yes, displays the Follow button:
All that is pretty neat, but people are lazy, and they tend to forget things, right? Why not allow users to fill in their contact information while they register. I do love WordPress registration form the way it is now, dead simple with only two fields (although, being able to set your own password would be nice – more on that in our post on allowing users to set password during registration), but if you’d really like your users to enter their additional contact info and don’t plan on making the fields mandatory, why not add them to WordPress registration form? Here’s how:
In the first block of code we hooked our custom function into a WordPress filter hook to modify WordPress’ output. This time, we’re using an action hook to hook our custom function into a WordPress action. Read more about add_action() function at the official WordPress Codex page.
Once again, that last block of code in plain English – “Hey WordPress, when you run register_form action, would you be so kind to show these fields as well? Thanks!“. Kinda like, “Honey, since you’re going out to a bar, could you take out the trash as well?” but more techy. It’s adding a function (taking out the trash) to your action (going out). The filter function is different, what it says is “Honey, now that you’re going to take out the trash, could you take this heavy old chair that we don’t need as well?” – you’re still taking out the trash, it’s just a little bit different – your output (pun, pun) is being filtered!
So now we can see and edit the fields in user profiles and we have some additional form fields in WordPress registration form. Next and final step would be to store those values into WordPress database, and in order to do that we need to hook into user_register action hook:
Yes, customizing WordPress registration form really is as simple as that, it took just 37 lines of code (without comments and empty lines, of course, I’m trying to make this look good!) to take care of:
- Removing some unused fields and adding Twitter, Facebook and Google+ contact information fields, hooking into user_contactmethods filter hook
- Adding those fields to WordPress registration form, hooking into register_form action hook
- Making sure they get stored in WordPress database when new users register, using user_register action hook
If you have an idea of how to improve this process, I’m sure you know how a comment form works :)
andreaamado Not exactly sure what you mean. If you implement those three blocks of code (first, third and fourth) you should have the fields in registration form. That's the one you meant when you said "create new user", right?
slobodanmanic
Thanks for your nice tutorial. But it doesn' t work for me too. The first block (filter) works just find but the register_form doesn't showing any new fileds in the »Add New User«. What could be wrong? The german language of the WP installation? I think not. But what else?
WebstudioSaar Thanks for pointing that out. Can you try again, using updated version of "Display additional contact fields in WordPress registration form" snippet, I just updated it to remove preset values, and there was a typo :(
Also, make sure you added all three blocks of code, it should work fine, I just tested it.
slobodanmanic Thanks, but no change. Caution of the line: twitter echo $_POST. There is missing a "<" before the ?
But anyway, no new fields in the backend.
WebstudioSaar Are you using updated version of that snippet? I removed all 'echo $_POST' references after your first comment, that's why I'm checking again.
slobodanmanic Yes, now without the $_POST. But unfortunately it doesn't work. For the profile just fine but not for a new user.
slobodanmanic Just trying on another WP install using version 3.3.2. No fields addet. :-(
WebstudioSaar Sorry it's not working :(
Could you clarify what you mean by "profile" and "new user". This code is supposed to add fields to registration form (check lead image in this post), which allow users to set Twitter, Facebook and Google+ details when they register. And for this to work, "Anyone has to register" option needs to be checked.
We'll make it work, just need to figure out exactly what the problem is.
slobodanmanic Oh, excuse me. Misunderstanding it. I thought I cound find the fileds in the backend below the button »All User« when you add a new one. This interpretation came about your answer of the first comment of this post. OK, now it works just over the front-end login area. Sorry again for that inconveniences.
WebstudioSaar No problem at all, sorry if the post wasn't clear enough! So, what you're trying to achieve is to add those fields to the form when you (an admin) are adding another user, right?
I'll cover that in an upcoming post. Checked it briefly now, the code for "Add user" page is in /wp-admin/user-new.php but I don't see any actions or filter hooks there.
In user-edit.php it's in line 355:
foreach (_wp_get_user_contactmethods( $profileuser ) as $name => $desc)
but contact methods are not shown anywhere in user-new.php so MAYBE this is impossible without hacking core files. I'll still look into it some more.
And thanks for checking out our blog :)
slobodanmanic Right, as an admin. Thanks for the suggestions. I'll try to playing around this and if I have any good results, I'll drop you a line.
andreaamado Now I know what you meant, thanks to WebstudioSaar :)
Check my other comments, this code only adds those fields in WordPress registration form, not "Add new user" page in dashboard.
nice! thank you!
I'm trying to ad a subscribe to the blog (by mail) option on the wordpress register form. Could you?
ericcarrerascandi Hi! Yeah, I guess that's possible, what would you be using to manage your mailing list? If it's a third party service, you can probably embed their signup form there as well, or you could try a WordPress plugin, like http://wordpress.org/extend/plugins/newsletter/
I hope that helps!
slobodanmanic I'm actualy using the jetpack subscriptions widget. I just want to put (maybe by code) a "sign-up to our newsletter" checkbox in the register form.
--> http://wordpress.org/extend/plugins/newsletter Thank u! I hope that it will work.
ericcarrerascandi Never used Jetpack subscriptions myself, so I'll have to look into it, but it's a nice post idea. Stay tuned, I'll try to cover it soon here, and sorry I can't help right away.
doesn't work to create new user, just for edit user. you know why?
thanks
- spam
- offensive
- disagree
- off topic
Like