Reputation: 1533
I have a wordpress website, and I want to add new fields when user register, like phone number and street address.
I know how to add input fields in registration form, but I cannot understand how to save this fields in database during registration, like other details as username are saved.
How to do that?
UPDATE:
add_action('user_register', 'myplugin_user_register')
the function myplugin_user_register() is called after main user details like username, email was inserted in database, or all details will be saved at same time.
I also want to save extra fields in other table, in $wpdb->users, how to do this ?
Upvotes: 1
Views: 13790
Reputation: 457
user_register action hook is used to save the extra field data to database as user meta submitted by custom registration forms.Basically , when a new user is created ,the user id is passed to this action hook as argument and used to save the extra field in user meta.
add_action( 'user_register', 'my_save_extra_fields', 10, 1 );
function my_save_extra_fields( $user_id ) {
if ( isset( $_POST['mobile_no'] ) )
update_user_meta($user_id, 'mobile_no', $_POST['mobile_no']);
}
Upvotes: 1
Reputation: 4544
Adding custom user field is like adding custom field to post.
It can be done with the help of the following filters :
You can see an example of the complete process here: http://codex.wordpress.org/Customizing_the_Registration_Form
If you need to allow the modification from the user from his profile page, check the filters personal_options_update and edit_user_profile_update.
Upvotes: 4
Reputation: 15949
In case you do not want to dip into code yourself, there are a few very simple plugins that will do the job for you like : cimy user extra fields or User Registration Aide..
You could search more at the same place ( Plugin repository )
Regarding your Update :
what you should use is update_user_meta()
, add_user_meta()
, delete_user_meta()
and get_user_meta()
, all of which act exactly like the post_meta ( custom fields ) with the sole difference that they are assigned to users instead of post . think of them like "custom fields for users"
and just like RafH said , there is no need to add tables, and actually it is a bad practice when working with wordpress, and a receipt for future headaches.
Upvotes: 1