This tutorial will show you how to add some extra information for your WordPress user profiles such as: Facebook URL, Twitter, Date Of Birth, Phone, Address, City, Province, Postal Code. Of course, you can add as much as extra fields as you want.
As you know, WordPress allows people to register to your site or the admin can add new user in the Dashboard with just some simple information related to their Name, Contact Info and Biographical Info.
However, in some case, you might need to know more about your users and ask them to provide some additional informations. To do that, just copy following codes below and paste them to the functions.php file in your theme directory (ex: “/wp-content/themes/twentyten/” where “twentyten” is your current theme name) or you have to create a new one if you don’t have it.
<?php add_action( 'show_user_profile', 'extra_user_profile_fields' ); add_action( 'edit_user_profile', 'extra_user_profile_fields' ); function extra_user_profile_fields( $user ) { ?> <h3><?php _e("Extra profile information", "blank"); ?></h3> <table class="form-table"> <tr> <th><label for="facebook"><?php _e("Facebook URL"); ?></label></th> <td> <input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your Facebook URL."); ?></span> </td> </tr> <tr> <th><label for="twitter"><?php _e("Twitter"); ?></label></th> <td> <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your Twitter Username."); ?></span> </td> </tr> <tr> <th><label for="dob"><?php _e("Date Of Birth"); ?></label></th> <td> <input type="text" name="dob" id="dob" value="<?php echo esc_attr( get_the_author_meta( 'dob', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your Date Of Birth, ex: 13/06/1983"); ?></span> </td> </tr> <tr> <th><label for="phone"><?php _e("Phone"); ?></label></th> <td> <input type="text" name="phone" id="phone" value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your Phone Number."); ?></span> </td> </tr> <tr> <th><label for="address"><?php _e("Address"); ?></label></th> <td> <input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your address."); ?></span> </td> </tr> <tr> <th><label for="city"><?php _e("City"); ?></label></th> <td> <input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your city."); ?></span> </td> </tr> <tr> <th><label for="province"><?php _e("Province"); ?></label></th> <td> <input type="text" name="province" id="province" value="<?php echo esc_attr( get_the_author_meta( 'province', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your province."); ?></span> </td> </tr> <tr> <th><label for="postalcode"><?php _e("Postal Code"); ?></label></th> <td> <input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( 'postalcode', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e("Please enter your postal code."); ?></span> </td> </tr> </table> <?php } add_action( 'personal_options_update', 'save_extra_user_profile_fields' ); add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' ); function save_extra_user_profile_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) { return false; } update_usermeta( $user_id, 'facebook', $_POST['facebook'] ); update_usermeta( $user_id, 'twitter', $_POST['twitter'] ); update_usermeta( $user_id, 'dob', $_POST['dob'] ); update_usermeta( $user_id, 'phone', $_POST['phone'] ); update_usermeta( $user_id, 'address', $_POST['address'] ); update_usermeta( $user_id, 'address', $_POST['address'] ); update_usermeta( $user_id, 'city', $_POST['city'] ); update_usermeta( $user_id, 'province', $_POST['province'] ); update_usermeta( $user_id, 'postalcode', $_POST['postalcode'] ); } ?> |
The PHP codes above will add a new table called “Extra profile information” to the Dashboard User Profile page which includes all extra information you want to add in. When you press Update Profile button, it will update all the extra fields one by one by calling the update_usermeta() function along with all default WordPress information.
+ Download my current functions.php file in Twentyten’s theme. I placed the PHP code above at the bottom of the file.