How To Add Custom Form Fields To The User Profile Page?

The user profile page has the following fields:

Username
First Name
Last Name
Nickname
Display name
Contact Info
E-mail
Website
AIM
Yahoo IM
Jabber / Google Talk

How can more fields be added to this section. Field such as Phone number, address, or anything else.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You need to use the 'show_user_profile', 'edit_user_profile', 'personal_options_update' and 'edit_user_profile_update' hooks.

Here’s some code to add a Phone number:

add_action( 'show_user_profile', 'yoursite_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'yoursite_extra_user_profile_fields' );
function yoursite_extra_user_profile_fields( $user ) {
?>
  <h3><?php _e("Extra profile information", "blank"); ?></h3>
  <table class="form-table">
    <tr>
      <th><label for="phone"><?php _e("Phone"); ?></label></th>
      <td>
        <input type="text" name="phone" id="phone" class="regular-text" 
            value="<?php echo esc_attr( get_the_author_meta( 'phone', $user->ID ) ); ?>" /><br />
        <span class="description"><?php _e("Please enter your phone."); ?></span>
    </td>
    </tr>
  </table>
<?php
}

add_action( 'personal_options_update', 'yoursite_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'yoursite_save_extra_user_profile_fields' );
function yoursite_save_extra_user_profile_fields( $user_id ) {
  $saved = false;
  if ( current_user_can( 'edit_user', $user_id ) ) {
    update_user_meta( $user_id, 'phone', $_POST['phone'] );
    $saved = true;
  }
  return true;
}

That code will add a field to your user screen that looks something like this:

skitched 20101114 000617

There are also several blog posts available on the subject that might be helpful:

Or if you prefer not to roll-your-own there are plugins that add said features such as the following (although I’m sure there are others):

Method 2

// remove aim, jabber, yim 
function hide_profile_fields( $contactmethods ) {
    unset($contactmethods['aim']);
    unset($contactmethods['jabber']);
    unset($contactmethods['yim']);
    return $contactmethods;
}

// add anything else
function my_new_contactmethods( $contactmethods ) {
    //add Birthday
    $contactmethods['birthday'] = 'Birthday';
    //add Address
    $contactmethods['address'] = 'Address';
    //add City
    $contactmethods['city'] = 'City';
    //add State
    $contactmethods['state'] = 'State';
    //add Postcode
    $contactmethods['postcode'] = 'Postcode';
    //add Phone
    $contactmethods['phone'] = 'Phone';
    //add Mobilphone
    $contactmethods['mphone'] = 'Mobilphone';

    return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);
add_filter('user_contactmethods','hide_profile_fields',10,1);

Hope this helps.

Source: WPBeginner


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x