by Bhumi // Dec 20,2012 // No comment
Sometimes you want to add extra fields into the default registration provided by wordpress.As we know,By default wordpress provide registration with some two-three fixed fields and for some reason we need to add some new fields into registration so, Today I am going to explain you how you can add extra fields into registration and profile page in wordpress.
For adding an extra fields, we need to create hook using an add_action function and you just need to place code into function.php file of theme and drag below PHP code.
Below action register_extra_fields is used to add extra field into the layout of registration.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php add_action('register_form','register_extra_fields'); function register_extra_fields(){ ?> <p> <label class="label01"><?php _e('Title') ?></label> <select name="user_sal" class="text-input" style="height:30px;width:263px" tabindex="7"> <option value="" >select</option> <option value="Mr" <?php if($_POST['user_sal']=='Mr') { ?> selected="selected" <?Php } ?>>Mr</option> <option value="Miss" <?php if($_POST['user_sal']=='Miss'){ ?> selected="selected" <?Php } ?>>Miss</option> </select> </p> <p> <label class="label01"><?php _e('First Name') ?></label> <input id="user_fname" type="text" size="25" value="<?php echo $_POST['first_name']; ?>" name="first_name" class="text-input" tabindex="8"/> </p> <p> <label class="label01"><?php _e('Last Name') ?></label> <input id="user_lname" type="text" size="25" value="<?php echo $_POST['last_name']; ?>" name="last_name" class="text-input" tabindex="9"/> </p> <p> <input type="hidden" name="status" value="0" /> </p> <?php } ?> |
Here, I have just used title,first name and last name as extra fields.
NOTE: Above code will be added after the existing fields in registration form but if you want to move it to top, you can change position of below code into wp-login.php file.
|
1 2 |
<?php do_action('register_form'); ?> |
Next, we need to call action for checking errors and required fields which is like below
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
add_action('register_post','check_fields_errors',10,3); function check_fields_errors($login, $email, $errors) { global $firstname, $lastname; if ($_POST['first_name'] == '') { $errors->add('empty_realname', '<strong>Error</strong>: Please enter First name'); } else { $firstname = $_POST['first_name']; } if ($_POST['last_name'] == '') { $errors->add('empty_realname', '<strong>Error</strong>: Please enter Last name'); } else { $firstname = $_POST['last_name']; } } |
Now,Let’s go ahead and create an action to add extra fields information into database.WordPress provides easiest way to add additional user information through the add_user_meta(), delete_user_meta(), get_user_meta(), update_user_meta() functions.This functions does not update the wp_users table, but update the wp_usermeta table. This is by default database layout provided by wordpress which allows one specific table to hold all additional user information.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
add_action('user_register', 'register_post_fields'); function register_post_fields($user_id, $password='', $meta=array()) { $userdata = array(); $userdata['ID'] = $user_id; $userdata['first_name'] = $_POST['first_name']; $userdata['last_name'] = $_POST['last_name']; $userdata['user_sal'] = $_POST['user_sal']; wp_update_user($userdata); update_usermeta( $user_id, 'first_name', $_POST['first_name'] ); update_usermeta( $user_id, 'last_name', $_POST['last_name'] ); update_usermeta( $user_id, 'user_sal', $_POST['user_sal'] ); } |
That’s it we are done with adding extra fields on the registration form / registration page. Now let’s move to profile page.
First of all, we need to add action for displaying fields into profile page and then for updating information into database
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php add_action( 'show_user_profile', 'display_extra_profile_fields' ); add_action( 'edit_user_profile', 'display_extra_profile_fields' ); function display_extra_profile_fields( $user ) { global $current_user; get_currentuserinfo(); ?> <h3 class="head_title"><?php _e('Extra Profile Information', 'frontendprofile'); ?></h3> <table class="form-table"> <tr> <th><label for="label01" class="lable_profile"><?php _e('Title', 'frontendprofile'); ?></label></th> <td> <select name="user_sal"> <option value="" >select</option> <option value="Mr" <?php if(esc_attr( get_the_author_meta( 'user_sal', $user->ID ) )=='Mr'){ ?> selected="selected" <?Php } ?>>Mr</option> <option value="Miss" <?php if(esc_attr( get_the_author_meta( 'user_sal', $user->ID ) )=='Miss'){ ?> selected="selected" <?Php } ?>>Miss</option> </select> <span class="description"><h3><?php _e('Please select your Title.', 'frontendprofile'); ?></h3></span> </td> </tr> </table> <?php } ?> |
Now, same like registration let’s update profile information into database using below action hooks:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
add_action( 'personal_options_update', 'update_extra_profile_fields' ); add_action( 'edit_user_profile_update', 'update_extra_profile_fields' ); function update_extra_profile_fields( $user_id ) { global $current_user,$wpdb; get_currentuserinfo(); if ( !current_user_can( 'edit_user', $user_id ) ) return false; if (in_array('administrator', $current_user->roles) || $current_user->data->ID==$user_id){ $userdata = array(); $userdata['ID'] = $user_id; $userdata['user_sal'] = $_POST['user_sal']; update_usermeta( $user_id, 'user_sal', $_POST['user_sal'] ); } } |
That’s all you are done with adding custom fields into registration and profile in wordpress
Thanks for reading and feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
To change content Type of mail in WordPress
Here is the quick article about to explain how you can change mail content type in wordpress.Wordpress has a function wp_mail to send mail from READ MORE
The Best E-Commerce Plugins for WordPress
Here is the new guest post by katie,here you will get idea about thw best E-Comerce plugins for WordPress. WordPress templates can make it fast READ MORE
To Delete Taxonomies from database in wordpess
Basically, Latest version of wordpress have by default 2 categories. Tags and categories. Few days back,my friend asked me ,how can i delete terms from READ MORE
Remove suffix from taxonomies in WordPress
Right, I’m back after break as I was busy in stack overflow.Today I’m a going to go over how to replace some text or suffix READ MORE
Tips to Choose Your Next WordPress Theme
In this guest post by Anny,you will get tips to Help Choose Your Next WordPress Theme WordPress templates can make it fast and simple to READ MORE
Be a Contributor at CreativeDev.Write an article or tutorial to the community and share some of your knowledge!