How to add extra field in the wordpress user profile

Hi Folks!

Sometimes we need to add extra fields in wordpress user profile page for some more information like website URL, bio or AIM etc. Well, there is an easy solution from admin dashboard if you use BuddyPress. You can add new profile fields from Users > Profile fields. But what if you don’t use BuddyPress? Well, you are still able to add extra fields with some line of codes.

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution.

Let’s jump in the code:


You may also read:  How to make your wordpress secure, wp config tips and many more
<?php
add_action( 'show_user_profile', 'extra_fields' );
add_action( 'edit_user_profile', 'extra_fields' );
add_action( 'personal_options_update', 'save_fields' );
add_action( 'edit_user_profile_update', 'save_fields' );
function extra_fields($user) {
?>
<h3><?php _e("Extra profile information", "DOMAIN"); /*DOMAIN = Lang domain for l10n (optional)*/ ?></h3>
<table class="form-table">
<tbody>
<tr>
<th><?php _e("Extra profile information", "DOMAIN"); /*DOMAIN = Lang domain for l10n (optional)*/ ?></th>
<td><input class="regular-text" id="facebook" type="text" name="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" /></td>
</tr>
</tbody>
</table>
<?php }
function save_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; } //Checking if the current user has ability to edit the user profile information
update_user_meta( $user_id, 'facebook', $_POST['facebook'] );
}
You may also read:  How to enable php mail() in your ubuntu

view raw

code.php

hosted with ❤ by GitHub

You may also like...

Leave a Reply

%d bloggers like this: