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 to you how you can add extra fields into registration and profile page in WordPress.
For adding 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 an 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 | <?php add_action('register_form','register_extra_fields'); function register_extra_fields(){ ?> <p> <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> <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> <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> <input type="hidden" name="status" value="0" /> <?php } ?> |
Here, I have just used the 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 3 | <?php do_action('register_form'); ?> |
Next, we need to call action for checking errors and required fields which are 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'] == '') { } 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 14 | add_action('user_register', 'register_post_fields'); function register_post_fields($user_id, $password='', $meta=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 25 | <?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 17 | 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.
Comments (8)