46

I'm not very good with computers/codes etc. I use a plugin that makes a registration form thingy and in that form I added country, age group, gender and so on. I click the option that will add the registerer into the wordpress user thingy. But when I try it, only the username and email show on the Users section on the backend.. Is there a way for the other fields to show on the users section?

I need them to show for statistical uses.

1
  • +1 for the intro - may end up borrowing it! :-D Commented Nov 12 at 19:02

4 Answers 4

106

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

You can use the following code for adding additional fields in User section

Code for adding extra fields in Edit User Section:

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="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="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 }

Code for saving extra fields details in database:

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 ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
        return;
    }
    
    if ( !current_user_can( 'edit_user', $user_id ) ) { 
        return false; 
    }
    update_user_meta( $user_id, 'address', $_POST['address'] );
    update_user_meta( $user_id, 'city', $_POST['city'] );
    update_user_meta( $user_id, 'postalcode', $_POST['postalcode'] );
}

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

7
  • Bravo this works great.
    – AVEbrahimi
    Commented Mar 15, 2018 at 13:31
  • 1
    This isn't storing data from my extra fields in the DB. Suggestions please? Thx.
    – b_dubb
    Commented Jul 3, 2019 at 16:12
  • @b_dubb, Can you please share your code? So i'll check and let you know. Commented Aug 2, 2019 at 14:38
  • I have resolved my issue but thanks for reaching out.
    – b_dubb
    Commented Aug 5, 2019 at 17:31
  • 1
    You should add nonce verification to this to avoid introducing security vulnerabilities. developer.wordpress.org/themes/theme-security/using-nonces Commented Jan 30, 2020 at 18:26
10

The Advanced Custom Fields Pro plugin will allow you to add fields to user profiles without any coding.

4
  • 6
    Only the pro version Commented Mar 4, 2019 at 12:16
  • 4
    There are free ways of doing this with PHP.
    – Drmzindec
    Commented Oct 15, 2019 at 9:05
  • 3
    Yep - definitely possible to code this in PHP without ACF if you prefer. My experience is that it takes 100+ lines of code and you need to worry about nonce verification, writing the HTML of the form, etc. Could take a few hours of coding vs. 5-10 min of setup in ACF. Probably depends on if you're using ACF Pro already on a project. Commented Oct 15, 2019 at 16:02
  • 3
    Wordpress should do this without asking you to hardcode html forms in php. I second ACF, it should be part of the core. You can also define fields with code and version it.
    – marek.m
    Commented Jan 30, 2020 at 14:13
7

You'd better use get_user_meta (instead of get_the_author_meta):

function extra_user_profile_fields( $user ) {
    $meta = get_user_meta($user->ID, 'meta_key_name', false);
}
1
  • both works with no problems! Commented Aug 18, 2020 at 19:04
5

You can also easily add additional fields to the existing "Contact Info" section of the user profile edit screen, and it doesn't require set up of other hooks to update the meta fields.

function custom_user_profile_contact_fields( $methods ) {
    $methods['phone'] = 'Phone number';
    $methods['location'] = 'Location';
    return $methods;
}
add_action( 'user_contactmethods', 'custom_user_profile_contact_fields' );
2
  • by far the simplest answer
    – RafaSashi
    Commented May 1, 2023 at 9:19
  • @RafaSashi It may be, but just keep in mind that sometimes more advanced fields are required and this method only works for using plain old single line text fields. ;) Commented May 3, 2023 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.