How do I add a field on the Users profile in WordPress? For example, country, age etc

July 17, 2019 0 comments
How do I add a field on the Users profile in WordPress? For example, country, age etc

Especially in the case of front-end developers, we frequently find it difficult to add extra custom fields. Most of them find it easy to use ACF plugin to avoid writing line of code and solving further or potential configuration.

If the client or company informs you that their customers need to be able to edit their profile, then you may face some problems, as I told you particularly if you’re not familiar with back-end programming. In this case, my tips and tricks may help you solve these kinds of issues.

You need to use the show_user_profileedit_user_profilepersonal_options_update, and edit_user_profile_update hooks.

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

Step 1

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 }

Step 2

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 ( !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’] );
}

Ashok kuikel

Ashok Kuikel is DevOps Engineer(Cloud Computing and Cyber Security), Entrepreneur working on Socio-Economic Development via Technology

He has been actively contributing as Joint Secretary of Federation of Computer Association of Nepal Kavre Chapter. Beside that, he is an official Global Peace Ambassador for Global Peace Chain, Nepal Chapter and Member of Internet Society, Nepal Chapter.

Above all, he enjoys learning about new trends and technologies and loves to share innovative ideas to contribute for the growth of the industry.

You can follow me on Social Media, GitHub, and via my Blog Channels.

Leave a Reply

Articles and Tutorials

We love writing about WordPress and latest plugins tutorials, WooCommerce stats, and much more.