Adding custom user profile fields in wordpress

Open your theme’s “functions.php” file and this PHP code in your file.

<?php
add_action( ‘show_user_profile’, ‘show_extra_profile_fields’, 1 );
add_action( ‘edit_user_profile’, ‘show_extra_profile_fields’, 1 );

function show_extra_profile_fields( $user ) { ?>

<h3><?php _e(‘Your Hotel Address’, ‘frontendprofile’); ?></h3>

<table class=”form-table”>

<tr>
<th><label for=”twitter”><?php _e(‘Address’, ‘frontendprofile’); ?></label></th>

<td>
<textarea name=”address” id=”address” class=”regular-text” rows=”6″ cols”30″ /><?php echo esc_attr( get_the_author_meta( ‘address’, $user->ID ) ); ?></textarea><br />
<span class=”description”><?php _e(‘Please fill you Hotel Address’, ‘frontendprofile’); ?></span>
</td>
</tr>

</table>
<?php } ?>

 This will create new textarea field in the user edit profile screen.

 Now saving the custom user fields.

We have displaying the custom field or extra field in the user edit profile so because of why this field will not be save when the user update the profile. To do this we need to use one more function to handle this. Paste this PHP code in your functions.php file

<?php 

add_action( ‘personal_options_update’, ‘save_extra_profile_fields’ );

add_action( ‘edit_user_profile_update’, ‘save_extra_profile_fields’ );

 

function save_extra_profile_fields( $user_id ) {

 

if ( !current_user_can( ‘edit_user’, $user_id ) )

return false;

 

update_usermeta( $user_id, ‘address’, $_POST[‘address’] );

} ?>

 


To seek challenging roles and to achieve excellence in the field of Open Source Web Technology with a profound spirit of commitment and dedication.

Share This Post

Related Articles

© 2017 Sumit Bansal. All rights reserved. Site Admin · Entries RSS · Comments RSS
Powered by WordPress · Designed by Theme Junkie