How To Add Custom Fields in User Profile WordPress

How To Add Custom Fields in User Profile wordpress
988 Views

WordPress is a powerful platform that allows users to customize and extend its features through the use of plugins and themes. One such customization you may want to make is to add custom fields to the user profile in WordPress.

Custom fields allow you to store additional information about a user, such as their social media handles or job title. In this article, we will show you how to add custom fields to the user profile in WordPress.

If you prefer not to use a plugin, you can add custom fields to user profiles by writing custom code. Here is an example of how you can do this:

  1. Add the following code to your theme’s functions.php file.
  2. Use the show_user_profile and edit_user_profile actions to display the custom fields on the user profile edit screen.
  3. Use the personal_options_update and edit_user_profile_update actions to save the custom field data when the user profile is updated.
<?php
add_action( 'show_user_profile', 'cxc_extra_user_profile_fields' );
add_action( 'edit_user_profile', 'cxc_extra_user_profile_fields' );

function cxc_extra_user_profile_fields( $user ) {
	$address = get_user_meta( $user->ID , 'address', true );
	$city = get_user_meta( $user->ID, 'city', true );
	$postalcode = get_user_meta( $user->ID , 'postalcode', true );
	$user_profile = ( $user_profile = get_user_meta( $user->ID, 'user_profile', true ) ) ? $user_profile : 'yes';
	?>

	<h3><?php _e( "Extra profile information", "cxc-codexcoach" ); ?></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 $address; ?>" 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 $city; ?>" 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 $postalcode; ?>" class="regular-text" /><br />
				<span class="description"><?php _e("Please enter your postal code."); ?></span>
			</td>
		</tr>
		<tr>
			<th><label for="user_profile"><?php _e("Show User Profile"); ?></label></th>
			<td>
				<ul>
					<li>
						<label>
							<input type="radio" value="yes" name="user_profile"<?php checked( $user_profile, 'yes' ) ?> /> Yes
						</label>
					</li>
					<li>
						<label>
							<input type="radio" value="no" name="user_profile"<?php checked( $user_profile, 'no' ) ?> /> No
						</label>
					</li>
				</ul>
			</td>
		</tr>
	</table>
	<?php
}
?>
<?php
add_action( 'personal_options_update', 'cxc_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'cxc_save_extra_user_profile_fields' );

function cxc_save_extra_user_profile_fields( $user_id ) {
	
	if( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ '_wpnonce' ], 'update-user_' . $user_id ) ) {
		return;
	}
	
	if( ! current_user_can( 'edit_user', $user_id ) ) {
		return;
	}

	update_user_meta( $user_id, 'city', sanitize_text_field( $_POST[ 'city' ] ) );
	update_user_meta( $user_id, 'address', sanitize_text_field( $_POST[ 'address' ] ) );
	update_user_meta( $user_id, 'postalcode', sanitize_text_field( $_POST[ 'postalcode' ] ) );
	update_user_meta( $user_id, 'user_profile', sanitize_text_field( $_POST[ 'user_profile' ] ) );

}
?>
<?php
add_filter( 'user_contactmethods', 'cxc_modify_user_contact_methods' );

function cxc_modify_user_contact_methods( $methods ) {

	// Add user contact methods
	$methods['skype']   = __( 'Skype'   ); // add custom fields Skype in contacinfo 
	$methods['twitter'] = __( 'Twitter' ); // add custom fields Twitter in contacinfo 
	$methods['linkedin'] = __( 'Linkdin' ); // add custom fields Linkdin in contacinfo 
	$methods['facebook'] = __( 'Facebook' ); // add custom fields Facebook in contacinfo 

	// Remove user contact methods
	unset( $methods['twitter']    ); // Remove custom fields twitter in contacinfo 
	unset( $methods['facebook'] ); // Remove custom fields facebook in contacinfo 

	return $methods;
}
?>

This is a basic example of how you can add custom fields to the user profile in WordPress without using a plugin. You can modify the code to add additional fields and customize the appearance of the fields on the user profile page.

FAQ:

What are custom fields in WordPress?

Custom fields in WordPress are a way to store additional information about a user, beyond the standard information such as username, email address, and password.

How can I add custom fields to user profiles in WordPress?

You can add custom fields to user profiles in WordPress by creating a new function in your theme’s functions.php file, and using the show_user_profile and edit_user_profile actions to add the custom fields to the user profile page.

Can I add custom fields to user profiles without using a plugin?

Yes, you can add custom fields to user profiles in WordPress without using a plugin by using the methods described above.

What is the get_user_meta function in WordPress?

The get_user_meta function in WordPress allows you to retrieve the custom data stored for a user and display it on your site. This function is useful for displaying the custom fields in the user’s profile on the front-end of your website.

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published. Required fields are marked *