Do you want to add a new “address” field to user profile? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add and display custom profile info in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
<? 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("Mailing details", "blank"); ?></h3> <table class="form-table"> <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 type address here."); ?></span> </td> </tr> </table> <? } 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, 'address', $_POST['address'] ); } ?>
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 19 best WordPress plugins for non-profits and how to set up Google analytics eCommerce tracking for WooCommerce.
Hey Kevin please let me know how can we add listbox and radio type input in the above snippet and how can we make certain fields required
Hi Amit, you only need to ask the question once I try and get to everything as fast as I can. I will add a snippet next week that will address this question about adding select and radio / checkboxes to the user profile section.
thanks Kevin
How can we add a Listbox and Radio type of fields in the above snippets and how can we make the certain fields required. Thanks in advance
How can we add a Listbox and Radio type of fields in the above snippets and how can we make the certain fields required. Thanks in advance
This code does’nt seem to work in 3.3.1, any ideas …
Got the error, it is just the the php tag error on line 14 it should be <?php
thanks a lot for these snippets, it is really helpful
Hi Amit, glad to see you got things working properly. Enjoy the snippets.
Brilliant! Your site has saved me hours of head scratching.
Thanks.
No problem, glad I could help.
[…] This post was mentioned on Twitter by wp_freak, WPSNIPP. WPSNIPP said: #wordpress Add & display custom profile info http://bit.ly/hTg1GX #blog please RT 🙂 […]