User profile: add custom fields

Adds custom profile fields to the admin Users area and the WooCommerce My Account Edit Profile page.

// Display Hooks For User Edit
add_action( 'show_user_profile', function( $user ) {

	do_action( 'ccom_profile_fields', $user );

} );

add_action( 'edit_user_profile', function( $user ) {

	do_action( 'ccom_profile_fields', $user );

} );

// Display Logic For User Edit
add_action( 'ccom_profile_fields', function( $user ) {

	// Get Field Value
	$ccom_custom_field = get_user_meta(
		$user->ID, 'ccom_custom_field', true
	);

	// Show Field
	printf(
		'
			<table class="form-table">
				<tr>
					<th><label for="ccom_custom_field">%s</label></th>
					<td>
						<input type="text" id="ccom_custom_field" name="ccom_custom_field" value="%s" />
					</td>
				</tr>
			</table>
		',
		'Custom Field',
		$ccom_custom_field
	);

} );

// My Account Edit Profile Form
add_action( 'woocommerce_edit_account_form', function() {

	// Get User Object
	$user = wp_get_current_user();

	?>

	<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
		<label for="favorite_color">Custom Field</label>
		<input type="text"
			class="woocommerce-Input woocommerce-Input--text input-text"
			name="ccom_custom_field" id="ccom_custom_field"
			value="<?php echo esc_attr( $user->ccom_custom_field ); ?>" />
	</p>

	<?php

} );

// My Account Edit Profile Save
add_action( 'woocommerce_save_account_details', function( $user_id ) {

	// Maybe Update Field
	if( isset( $_POST['ccom_custom_field'] ) ) {
		update_user_meta(
			$user_id,
			'ccom_custom_field',
			esc_attr( $_POST['ccom_custom_field'] )
		);
	}

} );

// Profile Update Hooks
add_action( 'personal_options_update', function( $user_id ) {

	do_action( 'ccom_profile_update', $user_id );

} );

add_action( 'edit_user_profile_update', function( $user_id ) {

	do_action( 'ccom_profile_update', $user_id );

} );

// Profile Update Logic
add_action( 'ccom_profile_update', function( $user_id ) {

	// Security Check
	if ( ! current_user_can( 'edit_user', $user_id ) ) {
		return false;
	}

	// Maybe Update Field
	if( isset( $_POST['ccom_custom_field'] ) ) {
		update_user_meta(
			$user_id,
			'ccom_custom_field',
			esc_attr( $_POST['ccom_custom_field'] )
		);
	}

} );

Instructions for User profile: add custom fields

  1. Log into a staging or locally hosted clone of your site.
  2. Install and activate Code Snippets plugin.
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the section above.
  5. Check to ensure formatting came over properly.
  6. Customize the code as desired.
  7. Add a meaningful title.
  8. Select whether to run on front-end or back-end (or both).
  9. Click “Save and Activate”.
  10. Test your site to ensure it works.
  11. Disable if any problems, or recover.
  12. Repeat for live environment.

Need help modifying User profile: add custom fields?

Contact me. I can help with fitting projects or refer to my partner.

License

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.

Disclaimer of warranty

Note: I may receive compensation for referrals.

WP Engine - A smarter way to WordPress
The best email marketing tool, responsive templates, automations, Worldwide support, tracking and reports, Benchmark Email, free plan available
Sell everywhere. Use Shopify to sell in-store and online.
Klaviyo partner badge
Okendo Partner, certified
WooCommerce, the most customizable eCommerce platform for building your online business. Click to get started.
Jetpack, a stronger, customizable site without sacrificing safety. Click to get started.