Adding Custom Fields to WordPress User Profile

The user profile of WordPress can be fairly easily adapted to add your own values??. So you can add the necessary fields according to your requirements. Here is how you do it, we add a field for the address and the content will be stored in the database. Various hooks in WordPress make sure that you only have to worry about the fields.

The function to store the entries is fb_save_custom_user_profile_fields() and here it is important to check the rights of each active user, only if the rights are available, in this case for editing users (edit_user), then data may be stored.
The actual saving of data takes place via update_usermeta().

It can pass all kinds of fields of course, the input field is just an example. It is advisable to outsource the functions in a Plugin, an alternative is also the functions.php of the Theme, but is certainly not the best way.

Each function of the small sample was referred to two hooks. Because WordPress differentiate between editing of user profiles and update personal data. This is done using the constant IS_PROFILE_PAGE. On default this constant is on TRUE and so would the hooks show_user_profile and personal_options_update be enough to bring in new fields and save them. But this can vary depending on the installation and this way the admin cannot maintain these new fields. In particular, two more hooks are needed. Otherwise there might be many cases, where the admin has to maintain data which the user doesn’t even see in his profile.

function fb_add_custom_user_profile_fields( $user ) {
?>
	<h3><?php _e('Extra Profile Information', 'your_textdomain'); ?></h3>
	<table class="form-table">
		<tr>
			<th>
				<label for="address"><?php _e('Address', 'your_textdomain'); ?>
			</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.', 'your_textdomain'); ?></span>
			</td>
		</tr>
	</table>
<?php }
function fb_save_custom_user_profile_fields( $user_id ) {
	if ( !current_user_can( 'edit_user', $user_id ) )
		return FALSE;
	update_usermeta( $user_id, 'address', $_POST['address'] );
}
add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

WordPress Snippet Plugin Xtreme One WordPress Framework
© WP Engineer Team, All rights reserved (Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)

More Info: Click here

Have your say

Adding Custom Fields to WordPress User Profile

The user profile of WordPress can be fairly easily adapted to add your own values??. So you can add the necessary fields according to your requirements. Here is how you do it, we add a field for the address and the content will be stored in the database. Various hooks in WordPress make sure that you only have to worry about the fields.

The function to store the entries is fb_save_custom_user_profile_fields() and here it is important to check the rights of each active user, only if the rights are available, in this case for editing users (edit_user), then data may be stored.
The actual saving of data takes place via update_usermeta().

It can pass all kinds of fields of course, the input field is just an example. It is advisable to outsource the functions in a Plugin, an alternative is also the functions.php of the Theme, but is certainly not the best way.

Each function of the small sample was referred to two hooks. Because WordPress differentiate between editing of user profiles and update personal data. This is done using the constant IS_PROFILE_PAGE. On default this constant is on TRUE and so would the hooks show_user_profile and personal_options_update be enough to bring in new fields and save them. But this can vary depending on the installation and this way the admin cannot maintain these new fields. In particular, two more hooks are needed. Otherwise there might be many cases, where the admin has to maintain data which the user doesn’t even see in his profile.

function fb_add_custom_user_profile_fields( $user ) {
?>
	<h3><?php _e('Extra Profile Information', 'your_textdomain'); ?></h3>
	<table class="form-table">
		<tr>
			<th>
				<label for="address"><?php _e('Address', 'your_textdomain'); ?>
			</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.', 'your_textdomain'); ?></span>
			</td>
		</tr>
	</table>
<?php }
function fb_save_custom_user_profile_fields( $user_id ) {
	if ( !current_user_can( 'edit_user', $user_id ) )
		return FALSE;
	update_usermeta( $user_id, 'address', $_POST['address'] );
}
add_action( 'show_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'edit_user_profile', 'fb_add_custom_user_profile_fields' );
add_action( 'personal_options_update', 'fb_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'fb_save_custom_user_profile_fields' );

WordPress Snippet Plugin Xtreme One WordPress Framework
© WP Engineer Team, All rights reserved (Digital Fingerprint: WPEngineer-be0254ce2b4972feb4b9cb72034a092d)

More Info: Click here


Fatal error: Cannot redeclare ci_comment_form() (previously declared in /home/xtoptube/public_html/wphub.biz/wp-content/themes/wp_informati5/comment-form.php:21) in /home/xtoptube/public_html/wphub.biz/wp-content/themes/wp_informati5/comment-form.php on line 101