How to add custom fields in admin edit user and profile in WordPress

How to add custom fields in admin edit user and profile in WordPress
628 Views
0
(0)

Do you want to add custom fields in the admin edit user and profile section in WordPress? If yes, then you have come to the right place. Custom fields allow you to store additional information about a user, such as their address, phone number, or any other information that you may need.

By adding custom fields, you can easily manage user information and improve the functionality of your website.

In this article, we’ll show you how to add custom fields in the admin edit user and profile section in WordPress.

<?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
}
?> 

Save the custom fields that have been added to the user profile in WordPress

<?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' ] ) );

}
?>

Store and access additional information about the users on the website.

<?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;
}
?>

The add_filter the function is used to add a filter to a WordPress action or filter. It takes two parameters: the first one is the name of the filter (in this case, user_contactmethods), and the second one is the name of the function that should be called when the filter is applied (in this case, cxc_modify_user_contact_methods).

The cxc_modify_user_contact_methods a function is a function that modifies the user contact methods. It takes one parameter, $methods which is an array of user contact methods.

FAQs

Can I add custom fields to the WordPress user profile page?

Yes, you can add custom fields to the WordPress user profile page by using a plugin or custom code. You can use the show_user_profile and edit_user_profile actions to display the custom fields on the user profile page and the personal_options_update and edit_user_profile_update actions to save the custom field data.

How do I display custom fields in a WordPress user profile?

To display custom fields in a WordPress user profile, you can use the get_user_meta function to retrieve the custom field data and then display it in your user profile template.

Can I use a plugin to add custom fields to the WordPress user edit page and profile?

Yes, there are many plugins available that can help you add custom fields to the WordPress user edit page and profile.

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

  • CodexCoach

    - Web Development Expert

    CodexCoach is a skilled tech educator known for easy-to-follow tutorials in coding, digital design, and software development. With practical tips and interactive examples, CodexCoach helps people learn new tech skills and advance their careers.

Leave a comment

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